0
0
Flaskframework~10 mins

Logging in production in Flask - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Logging in production
Start Flask App
Configure Logger
App Running
Event Occurs
Logger Records Event
Log Stored in File or External System
Monitor Logs for Issues
This flow shows how a Flask app starts, sets up logging, records events during runtime, and stores logs for monitoring.
Execution Sample
Flask
import logging
from flask import Flask
app = Flask(__name__)
logging.basicConfig(filename='app.log', level=logging.INFO)
@app.route('/')
def home():
    app.logger.info('Home page accessed')
    return 'Hello, world!'
This code sets up a Flask app with logging to a file and logs a message when the home page is accessed.
Execution Table
StepActionLogger StateLog OutputResult
1Start Flask appLogger configured, no logs yetNoneApp ready to receive requests
2Receive GET request at '/'Logger readyNonehome() function called
3home() logs info messageLogger has one INFO logINFO:flask.app:Home page accessedLog written to app.log
4home() returns responseLogger unchangedNoneClient receives 'Hello, world!'
5No more requestsLogger unchangedNoneApp waits for next event
💡 No more requests, app continues running waiting for events
Variable Tracker
VariableStartAfter Step 3Final
app.loggerConfigured with INFO levelContains 1 log entryContains 1 log entry
Key Moments - 2 Insights
Why don't we see log output immediately on the console?
Because logging is configured to write to a file ('app.log'), so logs are stored there instead of printing to the console. See execution_table step 3.
What happens if the log level is set to WARNING but we log an INFO message?
The INFO message will be ignored and not recorded because the logger only records messages at WARNING level or higher. This is why setting the correct log level is important.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 3. What does the logger do?
AIt prints the message to the console
BIt writes an INFO message to the log file
CIt ignores the message because it's too low level
DIt crashes the app
💡 Hint
Check the 'Log Output' column at step 3 in the execution_table
At which step does the Flask app respond to the client?
AStep 1
BStep 2
CStep 4
DStep 5
💡 Hint
Look at the 'Result' column for when the client receives a response
If we change logging level to WARNING, what happens to the INFO log in step 3?
AIt is ignored and not logged
BIt is still logged
CIt causes an error
DIt is logged twice
💡 Hint
Refer to key_moments about log levels and filtering
Concept Snapshot
Flask logging in production:
- Configure logging with logging.basicConfig(filename, level)
- Use app.logger.info/debug/warning to log events
- Logs go to file or external system, not console by default
- Set appropriate log level to filter messages
- Logs help monitor app behavior and troubleshoot
Full Transcript
This visual execution shows how a Flask app sets up logging to a file and records an info message when the home page is accessed. The app starts, configures the logger to write INFO level logs to 'app.log', then when a client requests the home page, the app logs 'Home page accessed' to the file. The client receives the response 'Hello, world!'. The logger state changes from empty to having one log entry. Key points include understanding that logs go to a file, not console, and that log levels control which messages are recorded. The quiz tests understanding of when logs are written, when responses are sent, and how log levels affect logging.