0
0
Flaskframework~15 mins

Logging configuration in Flask - Mini Project: Build & Apply

Choose your learning style9 modes available
Logging configuration
📖 Scenario: You are building a simple Flask web application. To keep track of what happens in your app, you want to set up logging. Logging helps you see messages about your app's activity, like errors or important events.
🎯 Goal: Set up basic logging in a Flask app to record messages with different severity levels. You will create a logger, configure its level, and log a test message.
📋 What You'll Learn
Create a Flask app instance named app
Set up a logger named app.logger
Configure the logger level to logging.INFO
Log an info message 'App started'
💡 Why This Matters
🌍 Real World
Logging is essential in real web applications to monitor app behavior, catch errors, and understand user activity.
💼 Career
Knowing how to configure logging in Flask is a basic skill for backend developers and DevOps engineers to maintain and troubleshoot web services.
Progress0 / 4 steps
1
Create a Flask app instance
Import Flask from flask and create a Flask app instance called app.
Flask
Need a hint?

Use Flask(__name__) to create the app instance.

2
Import logging and set logger level
Import the logging module, configure basic logging with logging.basicConfig(level=logging.INFO), and set the app.logger level to logging.INFO.
Flask
Need a hint?

Use logging.basicConfig(level=logging.INFO) and app.logger.setLevel(logging.INFO).

3
Log an info message
Use app.logger.info to log the message 'App started'.
Flask
Need a hint?

Call app.logger.info('App started') to log the message.

4
Run the Flask app and see the log output
Add the code to run the Flask app only if the script is run directly. Use app.run(). When you run the app, the info log message 'App started' should appear in the console.
Flask
Need a hint?

Use the standard Python check if __name__ == '__main__': to run the app.