Practice: Logging

« Read the chapter

Q1. Log a warning

BeginnerCoding

Configure logging to print to stdout with format 'LEVEL: message' and log the warning 'low battery'.

Goal: Use basicConfig and a log level.

Q2. Hide debug messages

BeginnerCoding

Set the level to INFO, then log one debug and one info message. Only the info message should appear.

Goal: Understand the threshold.

Q3. Lazy formatting

IntermediateCoding

Log the message 'user Asha has 3 points' using lazy %s and %d arguments at INFO level.

Goal: Use lazy formatting.

Q4. Named logger

IntermediateCoding

Create a logger named 'db' and log an error 'connection lost' with format '%(name)s - %(message)s'.

Goal: Create named loggers.

Q5. Write to a file

IntermediateCoding

Log 'started' at INFO to a file named run.log using basicConfig(filename=...), then print the file contents.

Goal: Log to a file.

Q6. Log a caught error

AdvancedCoding

Write a function that divides 10 by a number. If it fails with ZeroDivisionError, log 'cannot divide' at ERROR level and return 0. Call it with 0 and print the result.

Goal: Log exceptions.

Q7. Default threshold

BeginnerOutput

What does this print?

Code
import logging, sys
logging.basicConfig(stream=sys.stdout, force=True)
logging.info('one')
logging.warning('two')

Goal: Know the default level.

Q8. Levels shown

BeginnerOutput

What does this print?

Code
import logging, sys
logging.basicConfig(stream=sys.stdout, level=logging.ERROR, format='%(levelname)s', force=True)
logging.warning('a')
logging.error('b')
logging.critical('c')

Goal: Apply the threshold.

Q9. Format string

IntermediateOutput

What does this print?

Code
import logging, sys
logging.basicConfig(stream=sys.stdout, level=logging.INFO, format='[%(name)s] %(message)s', force=True)
logging.getLogger('x.y').info('hi')

Goal: Read format strings.

Q10. Level numbers

IntermediateOutput

What does this print?

Code
import logging
print(logging.INFO + logging.DEBUG, logging.CRITICAL)

Goal: Know level numbers.

Q11. Logger identity

IntermediateOutput

What does this print?

Code
import logging
a = logging.getLogger('app')
b = logging.getLogger('app')
print(a is b, a.name)

Goal: Understand getLogger.

Q12. Child logger level

AdvancedOutput

What does this print?

Code
import logging, sys
logging.basicConfig(stream=sys.stdout, format='%(message)s', force=True)
log = logging.getLogger('a.b')
log.setLevel(logging.DEBUG)
log.debug('child')
logging.getLogger('a').debug('parent')

Goal: Understand logger levels.

Q13. Nothing appears

BeginnerDebugging

This code should show the info line but prints nothing. Fix it.

Code
import logging
logging.info('hello')

Goal: Fix level problems.

Q14. Late basicConfig

IntermediateDebugging

The debug line never appears because basicConfig is called after a logging call. Fix it so DEBUG shows.

Code
import logging, sys
logging.warning('start')
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG, format='%(message)s')
logging.debug('details')

Goal: Order of configuration.

Q15. Repeated lines

IntermediateConceptual

Why can adding a handler inside a function cause repeated lines, and what is the fix?

Goal: Avoid duplicate handlers.

Q16. print versus logging

BeginnerConceptual

Give two advantages of logging over print for a long-running service.

Goal: Compare print and logging.

Q17. Choose a level

IntermediateApplication

A web app cannot connect to its database and the current request fails, but the app keeps running. Which level fits best and why?

Goal: Pick levels.

Q18. Logging in a library

AdvancedApplication

Should a reusable library call basicConfig? Explain.

Goal: Design for libraries.