This answer key contains 4 multiple-choice, 4 fill-in-the-blank, and 4 output-prediction questions for Python Logging, with the exact answer and a short explanation for every question.
The answers are shown in full, so use this page after making an attempt. The practice page is the better first pass.
Where does a logger send its records?
Answer: To its handlers
Why: Handlers own the destinations; a logger passes records to them.
What is the default level of the root logger?
Answer: WARNING
Why: Without configuration, the root logger normally handles WARNING and above.
What happens when logger.info is called at WARNING level?
Answer: Nothing is emitted by that logger
Why: INFO is below the WARNING threshold, so the record is filtered out.
What does a formatter do?
Answer: Turns a record into text
Why: The formatter controls the final text layout for a handler.
Complete the level used for informational messages.
import logging
logger = logging.getLogger("demo")
logger.setLevel(logging.____)Answer: INFO
Why: logging.INFO is the informational severity.
Complete the handler that writes to a stream.
import logging
from io import StringIO
stream = StringIO()
handler = logging.____(stream)Answer: StreamHandler
Why: StreamHandler sends records to a stream such as StringIO.
Complete the class that controls the text layout.
import logging
from io import StringIO
handler = logging.StreamHandler(StringIO())
handler.setFormatter(logging.____("%(levelname)s:%(message)s"))Answer: Formatter
Why: Formatter turns a record into the final string.
Complete the value that stops propagation to the root logger.
import logging
logger = logging.getLogger("demo")
logger.propagate = ____Answer: False
Why: Setting propagate to False keeps the record in this logger's handler chain.
What does this code print?
import logging
from io import StringIO
stream = StringIO()
handler = logging.StreamHandler(stream)
logger = logging.getLogger("demo")
logger.handlers[:] = [handler]
logger.setLevel(logging.INFO)
logger.propagate = False
logger.info("hello")
print(stream.getvalue().strip())Answer: hello
Why: The handler receives the info record and writes hello to the stream.
What does the program output?
import logging
from io import StringIO
stream = StringIO()
logger = logging.getLogger("demo")
logger.handlers[:] = [logging.StreamHandler(stream)]
logger.setLevel(logging.WARNING)
logger.propagate = False
logger.info("hidden")
logger.warning("shown")
print(stream.getvalue().strip())Answer: shown
Why: The info record is below the threshold, while the warning record is emitted.
What appears on the screen?
import logging
from io import StringIO
stream = StringIO()
handler = logging.StreamHandler(stream)
handler.setFormatter(logging.Formatter("%(levelname)s:%(message)s"))
logger = logging.getLogger("demo")
logger.handlers[:] = [handler]
logger.setLevel(logging.INFO)
logger.propagate = False
logger.warning("check")
print(stream.getvalue().strip())Answer: WARNING:check
Why: The formatter puts the level name before the message.
What is printed?
import logging
print(logging.getLevelName(20))Answer: INFO
Why: Level 20 has the name INFO.
Try the questions first
Open the practice page for Python Logging and answer the same 12 questions with instant feedback.