0
0
Flaskframework~30 mins

Logging in production in Flask - Mini Project: Build & Apply

Choose your learning style9 modes available
Logging in Production with Flask
📖 Scenario: You are building a simple Flask web application that will run in production. To keep track of what happens in your app, you need to set up logging. Logging helps you see errors and important events after your app is deployed.
🎯 Goal: Set up a basic logging system in a Flask app that writes messages to a file with a specific format. You will create the app, configure logging, add a log message when the home page is visited, and then print a confirmation message.
📋 What You'll Learn
Create a Flask app instance named app
Configure logging to write to a file named app.log
Set logging level to INFO
Use a log format that includes time, level, and message
Add a log message inside the home route when it is accessed
Print "Logging setup complete" at the end
💡 Why This Matters
🌍 Real World
Logging is essential in production apps to monitor behavior and troubleshoot issues after deployment.
💼 Career
Understanding how to set up and use logging in web applications is a key skill for DevOps and backend developers.
Progress0 / 4 steps
1
Create the Flask app
Import Flask from flask and create a Flask app instance called app.
Flask
Need a hint?

Use Flask(__name__) to create the app.

2
Configure logging to a file
Import logging and configure it to write logs to a file named app.log with level INFO and format showing time, level, and message.
Flask
Need a hint?

Use logging.basicConfig with filename, level, and format parameters.

3
Add a log message in the home route
Create a route for "/" using @app.route("/"). Define a function home that logs an INFO message "Home page accessed" using logging.info and returns the string "Welcome to the home page!".
Flask
Need a hint?

Use @app.route("/") to define the route and logging.info inside the function.

4
Print confirmation message
Write a print statement to display the text "Logging setup complete".
Flask
Need a hint?

Use print("Logging setup complete") to show the message.