Logging helps you keep track of what your Flask app is doing. It records important events and errors so you can understand and fix problems.
Logging configuration in Flask
import logging from flask import Flask app = Flask(__name__) logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s: %(message)s') @app.route('/') def home(): app.logger.info('Home page accessed') return 'Hello, Flask!' if __name__ == '__main__': app.run()
logging.basicConfig sets up the logging system with level and format.
app.logger is Flask's built-in logger to write messages.
logging.basicConfig(level=logging.DEBUG)
app.log.logging.basicConfig(filename='app.log', level=logging.WARNING)app.logger.error('An error happened!')This Flask app logs an info message every time the home page is visited. The log shows the time, level, and message.
import logging from flask import Flask app = Flask(__name__) logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s: %(message)s') @app.route('/') def home(): app.logger.info('Home page accessed') return 'Hello, Flask!' if __name__ == '__main__': app.run()
Use different logging levels: DEBUG, INFO, WARNING, ERROR, CRITICAL to control message importance.
Logging to a file helps keep records even after the app stops.
Flask's app.logger is ready to use and integrates with Python's logging module.
Logging records app events and errors to help debugging and monitoring.
Use logging.basicConfig to set up logging level and format.
Use app.logger in Flask to write log messages easily.
Practice
Solution
Step 1: Understand logging purpose
Logging helps track what happens inside the app, especially errors and events.Step 2: Match purpose to options
Only To record app events and errors for debugging and monitoring describes recording events and errors for debugging and monitoring.Final Answer:
To record app events and errors for debugging and monitoring -> Option BQuick Check:
Logging = record events/errors [OK]
- Thinking logging speeds up the app
- Believing logging fixes bugs automatically
- Confusing logging with UI changes
logging.basicConfig in Flask?Solution
Step 1: Recall logging.basicConfig syntax
The correct syntax requires the keyword argument 'level' with a logging constant like logging.DEBUG.Step 2: Check each option
logging.basicConfig(level=logging.DEBUG) uses level=logging.DEBUG correctly. logging.basicConfig(DEBUG) misses 'level='. logging.basicConfig(level='DEBUG') uses a string instead of logging.DEBUG constant. logging.basicConfig(loglevel=logging.DEBUG) uses wrong keyword 'loglevel'.Final Answer:
logging.basicConfig(level=logging.DEBUG) -> Option DQuick Check:
Use level=logging.DEBUG [OK]
- Omitting 'level=' keyword
- Passing level as string instead of logging constant
- Using wrong keyword like 'loglevel'
import logging
from flask import Flask
app = Flask(__name__)
logging.basicConfig(level=logging.INFO)
@app.route('/')
def home():
app.logger.debug('Debug message')
app.logger.info('Info message')
return 'Hello!'
if __name__ == '__main__':
app.run()Solution
Step 1: Check logging level set
logging.basicConfig(level=logging.INFO) means only INFO and higher levels show, DEBUG is lower and ignored.Step 2: Analyze logged messages
app.logger.debug('Debug message') is below INFO, so ignored. app.logger.info('Info message') is at INFO level, so it appears.Final Answer:
Only 'Info message' will appear in the console -> Option CQuick Check:
Level INFO hides DEBUG [OK]
- Assuming debug messages always show
- Confusing logging levels order
- Forgetting to set logging level
import logging
from flask import Flask
app = Flask(__name__)
logging.basicConfig(format='%(levelname)s:%(message)s')
@app.route('/')
def home():
app.logger.warning('Warning message')
return 'Hi'
What is the likely problem?Solution
Step 1: Understand logging.basicConfig behavior
basicConfig only works if no logging configured before. Flask may configure logging before this call.Step 2: Identify timing issue
Calling basicConfig after Flask app creation may not set level or format properly, so logs don't appear.Final Answer:
Logging level is not set, defaults to WARNING but basicConfig called too late -> Option AQuick Check:
Call basicConfig before app creation [OK]
- Setting format but not level
- Calling basicConfig after app creation
- Thinking debug=True controls logging output
app.log with level WARNING or higher and a custom format showing time, level, and message. Which code snippet correctly configures this in Flask?Solution
Step 1: Check filename argument
Correct argument is 'filename', not 'file'. Options B is wrong.Step 2: Check level argument type
Level must be logging.WARNING constant, not string. Options B and C use string, wrong.Step 3: Check format string placeholders
Correct placeholders are %(asctime)s, %(levelname)s, %(message)s. Options A and B use wrong placeholders.Final Answer:
logging.basicConfig(filename='app.log', level=logging.WARNING, format='%(asctime)s %(levelname)s %(message)s') -> Option AQuick Check:
Use filename, logging.WARNING, correct format placeholders [OK]
- Using 'file' instead of 'filename'
- Passing level as string instead of logging constant
- Wrong format placeholders like %(time)s
Ready to prove it?
Pass the test and this pattern moves unknown
