Skip to content
Foxipa
Code Completion 4 Challenges

Complete the Python logging code

Practice recording events with correct levels, deferred message arguments, exception tracebacks, and formatter configuration.

Intermediate ~9 min read

Challenge 1

Record the number of loaded records using a logging message template and separate argument.

python Your answer
import logging
logger = logging.getLogger(__name__)
record_count = 12

Press Ctrl⌘Enter to check your answer

Challenge 2

Configure the logging threshold so INFO and higher-severity records are handled.

python Your answer
import logging
logging.basicConfig(level=, format="%(levelname)s:%(message)s")
logging.getLogger(__name__).info("Started")

Press Ctrl⌘Enter to check your answer

Challenge 3

Inside the shown except block, record the failure and its active traceback.

python Your answer
import logging
logger = logging.getLogger(__name__)
raw = "invalid"
try:
    int(raw)
except ValueError:
    logger.

Press Ctrl⌘Enter to check your answer

Challenge 4

Format each record with its level name followed by its message.

python Your answer
import logging
handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter())

Press Ctrl⌘Enter to check your answer