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?
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'
Remember the logging level set in basicConfig controls which messages appear.
The logging level is set to WARNING, so only messages with level WARNING or higher are shown. The debug message is ignored.
Choose the correct Flask logging setup that writes all INFO and above messages to a file named app.log.
Check the parameter names and level types in basicConfig.
Option A correctly sets filename, level as logging.INFO, and a proper format string. Option A uses wrong parameter 'file'. Option A sets level as string instead of logging constant. Option A sets level DEBUG, which is more verbose than requested.
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?
Flask uses its own logger instance which may have a different level than root logger.
Flask's app.logger has its own level set to WARNING by default. Setting root logger level with basicConfig does not change app.logger's level. You must set app.logger.setLevel(logging.DEBUG) explicitly.
Arrange these steps in the correct order to achieve the logging setup:
Think about setting the logger level before adding handlers and the order of creating handlers.
First set logger level to INFO to allow INFO and above. Then create StreamHandler for INFO logs, FileHandler for ERROR logs, then add both handlers to logger.
In production, Flask logs sometimes appear duplicated in console and files. Which practice prevents this?
Propagation causes logs to be passed to parent loggers, causing duplicates.
By default, Flask's logger propagates messages to the root logger, which may also have handlers. Disabling propagation prevents duplicate logs.