0
0
Flaskframework~10 mins

Logging configuration in Flask - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Logging configuration
Start Flask App
Setup Logging Config
Create Logger Object
Set Logger Level
Add Handlers (Console/File)
Log Messages
Messages Output to Handlers
App Runs with Logging Enabled
This flow shows how a Flask app sets up logging: configure, create logger, set level, add handlers, then log messages output.
Execution Sample
Flask
import logging
from flask import Flask
app = Flask(__name__)
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
logger.info('App started')
This code sets up basic logging in Flask and logs an info message when the app starts.
Execution Table
StepActionLogger LevelHandlers AttachedMessage LoggedOutput
1Import logging and FlaskNOT SETNoneNoneNo output
2Create Flask app instanceNOT SETNoneNoneNo output
3Call logging.basicConfig(level=logging.INFO)INFODefault StreamHandler (console)NoneNo output
4Get logger with getLogger(__name__)INFODefault StreamHandler (console)NoneNo output
5Call logger.info('App started')INFODefault StreamHandler (console)'App started'Printed on console: 'App started'
6App runs with logging enabledINFODefault StreamHandler (console)Any further logsLogs appear on console
💡 Logging configured at INFO level; messages below INFO are ignored; app runs with logging active
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 5Final
logger.levelNOT SETINFOINFOINFOINFO
logger.handlersNoneStreamHandler(console)StreamHandler(console)StreamHandler(console)StreamHandler(console)
messageNoneNoneNone'App started'Any logged messages
Key Moments - 3 Insights
Why do we not see any output before calling logger.info()?
Because logging only outputs messages when a log method like info() is called, as shown in execution_table step 5.
What happens if we set the logging level to WARNING instead of INFO?
Messages with level INFO would be ignored and not output, as logging only shows messages at or above the set level (see exit_note).
Why do we use getLogger(__name__) instead of root logger?
Using getLogger(__name__) creates a logger specific to the module, allowing better control and filtering, as seen in variable_tracker logger handlers.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 5, what is the logger level when 'App started' is logged?
ADEBUG
BWARNING
CINFO
DERROR
💡 Hint
Check the 'Logger Level' column at step 5 in the execution_table.
At which step are handlers attached to the logger?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Handlers Attached' column in execution_table to see when handlers appear.
If we change logging.basicConfig(level=logging.WARNING), what happens to the message logged at step 5?
AIt will not print because INFO < WARNING
BIt will cause an error
CIt will still print on console
DIt will print twice
💡 Hint
Refer to the exit_note about logging levels filtering messages below the set level.
Concept Snapshot
Logging configuration in Flask:
- Import logging and call logging.basicConfig(level=LEVEL)
- Create logger with getLogger(__name__)
- Set level controls which messages show
- Add handlers to output logs (console/file)
- Use logger.info/debug/error to log messages
- Messages below level are ignored
Full Transcript
This visual execution shows how to configure logging in a Flask app. First, the logging module and Flask are imported. Then, the Flask app instance is created. Next, logging.basicConfig is called to set the logging level to INFO and attach a default console handler. A logger specific to the module is created with getLogger(__name__). When logger.info is called with 'App started', the message is output to the console because the level is INFO. Variables like logger.level and handlers are tracked to show their state changes. Key moments clarify why messages appear only after logging calls and how levels filter output. The quiz tests understanding of logger levels, handler attachment, and message filtering. The snapshot summarizes the steps to configure and use logging in Flask.