Answer key · 12 questions

Python Logging Exercises With Answers

All 12 questions for Python Logging in one place. Try the practice page first, then use this page to check the exact answer and the reason.

Quick answer

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.

01 · Multiple choice

Where does a logger send its records?

  1. To its handlerscorrect
  2. Only to the terminal
  3. To a list of strings
  4. To a database by default

Answer: To its handlers

Why: Handlers own the destinations; a logger passes records to them.

02 · Multiple choice

What is the default level of the root logger?

  1. DEBUG
  2. ERROR
  3. INFO
  4. WARNINGcorrect

Answer: WARNING

Why: Without configuration, the root logger normally handles WARNING and above.

03 · Multiple choice

What happens when logger.info is called at WARNING level?

  1. The level changes to INFO
  2. The message is printed twice
  3. An error is raised
  4. Nothing is emitted by that loggercorrect

Answer: Nothing is emitted by that logger

Why: INFO is below the WARNING threshold, so the record is filtered out.

04 · Multiple choice

What does a formatter do?

  1. Changes the logger level
  2. Deletes old handlers
  3. Turns a record into textcorrect
  4. Stores records in a file

Answer: Turns a record into text

Why: The formatter controls the final text layout for a handler.

05 · Fill in the blank

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.

06 · Fill in the blank

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.

07 · Fill in the blank

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.

08 · Fill in the blank

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.

09 · Predict output

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.

10 · Predict output

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.

11 · Predict output

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.

12 · Predict output

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.

Practice this lesson