0
0
Flaskframework~20 mins

Logging configuration in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Logging Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this Flask logging setup?

Consider this Flask app snippet configuring logging:

import logging
from flask import Flask

app = Flask(__name__)

logging.basicConfig(level=logging.WARNING, format='%(levelname)s:%(message)s')

@app.route('/')
def home():
    app.logger.debug('Debug message')
    app.logger.warning('Warning message')
    return 'Hello'

What will be printed to the console when accessing the '/' route?

Flask
import logging
from flask import Flask

app = Flask(__name__)

logging.basicConfig(level=logging.WARNING, format='%(levelname)s:%(message)s')

@app.route('/')
def home():
    app.logger.debug('Debug message')
    app.logger.warning('Warning message')
    return 'Hello'
AWARNING:Warning message
BDEBUG:Debug message\nWARNING:Warning message
CNo output printed
DERROR:Warning message
Attempts:
2 left
💡 Hint

Remember the logging level set in basicConfig controls which messages appear.

Configuration
intermediate
2:00remaining
Which logging configuration writes logs to a file with INFO level?

Choose the correct Flask logging setup that writes all INFO and above messages to a file named app.log.

Alogging.basicConfig(filename='app.log', level=logging.INFO, format='%(asctime)s %(levelname)s %(message)s')
Blogging.basicConfig(filename='app.log', level=logging.DEBUG, format='%(message)s')
Clogging.basicConfig(file='app.log', level=logging.INFO, format='%(levelname)s %(message)s')
Dlogging.basicConfig(filename='app.log', level='INFO', format='%(asctime)s %(message)s')
Attempts:
2 left
💡 Hint

Check the parameter names and level types in basicConfig.

Troubleshoot
advanced
2:00remaining
Why does Flask not log messages below WARNING despite setting DEBUG level?

A developer sets Flask logging level to DEBUG using:

logging.basicConfig(level=logging.DEBUG)

But only WARNING and above messages appear in console. What is the most likely cause?

AThe format string is missing, so DEBUG messages are suppressed
BFlask's default logger level overrides basicConfig and remains at WARNING
CThe app.logger.debug() method is deprecated and does not log messages
DThe logging module does not support DEBUG level in Flask
Attempts:
2 left
💡 Hint

Flask uses its own logger instance which may have a different level than root logger.

🔀 Workflow
advanced
3:00remaining
Order the steps to configure Flask logging to send ERROR logs to a file and INFO logs to console.

Arrange these steps in the correct order to achieve the logging setup:

A2,1,4,3
B4,1,2,3
C1,2,4,3
D4,2,1,3
Attempts:
2 left
💡 Hint

Think about setting the logger level before adding handlers and the order of creating handlers.

Best Practice
expert
3:00remaining
Which is the best practice for Flask logging in production to avoid duplicate log entries?

In production, Flask logs sometimes appear duplicated in console and files. Which practice prevents this?

AAdd multiple handlers without removing existing ones
BCall logging.basicConfig() multiple times to reset handlers
CDisable Flask's default logger propagation by setting app.logger.propagate = False
DSet logging level to DEBUG to reduce duplicates
Attempts:
2 left
💡 Hint

Propagation causes logs to be passed to parent loggers, causing duplicates.