Practice: Logging
Q1. Log a warning
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
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
Log the message 'user Asha has 3 points' using lazy %s and %d arguments at INFO level.
Goal: Use lazy formatting.
Q4. Named logger
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
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
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
What does this print?
import logging, sys
logging.basicConfig(stream=sys.stdout, force=True)
logging.info('one')
logging.warning('two')Goal: Know the default level.
Q8. Levels shown
What does this print?
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
What does this print?
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
What does this print?
import logging
print(logging.INFO + logging.DEBUG, logging.CRITICAL)Goal: Know level numbers.
Q11. Logger identity
What does this print?
import logging
a = logging.getLogger('app')
b = logging.getLogger('app')
print(a is b, a.name)Goal: Understand getLogger.
Q12. Child logger level
What does this print?
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
This code should show the info line but prints nothing. Fix it.
import logging
logging.info('hello')Goal: Fix level problems.
Q14. Late basicConfig
The debug line never appears because basicConfig is called after a logging call. Fix it so DEBUG shows.
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
Why can adding a handler inside a function cause repeated lines, and what is the fix?
Goal: Avoid duplicate handlers.
Q16. print versus logging
Give two advantages of logging over print for a long-running service.
Goal: Compare print and logging.
Q17. Choose a level
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
Should a reusable library call basicConfig? Explain.
Goal: Design for libraries.