📖 Scenario: You are building a simple FastAPI 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 actions, errors, and important events.
🎯 Goal: Set up basic logging in a FastAPI app that writes messages to the console with a specific format.
📋 What You'll Learn
Create a logger named app_logger
Set the logging level to INFO
Configure the logger to output messages with the format: %(levelname)s:%(message)s
Log an INFO message saying App started
💡 Why This Matters
🌍 Real World
Logging is essential in real applications to monitor what the app is doing and to help find problems quickly.
💼 Career
Understanding logging setup is a key skill for DevOps and backend developers to maintain and troubleshoot applications.
Progress0 / 4 steps
1
Create a FastAPI app and import logging
Write code to import the FastAPI class from fastapi and the logging module. Then create a FastAPI app instance called app.
FastAPI
Hint
Use from fastapi import FastAPI and app = FastAPI() to create the app.
2
Create a logger named app_logger and set level to INFO
Create a logger called app_logger using logging.getLogger with the name app_logger. Set its logging level to logging.INFO.
FastAPI
Hint
Use logging.getLogger('app_logger') and setLevel(logging.INFO).
3
Add a console handler with a simple message format
Create a StreamHandler for app_logger to output logs to the console. Set its format to %(levelname)s:%(message)s using a Formatter. Add this handler to app_logger.
FastAPI
Hint
Create a StreamHandler, set its formatter, then add it to app_logger.
4
Log an INFO message 'App started'
Use app_logger.info to log the message 'App started'.
FastAPI
Hint
Call app_logger.info('App started') to log the message.
Practice
(1/5)
1. What is the main purpose of configuring logging in a FastAPI application?
easy
A. To track app activity and errors for easier debugging
B. To speed up the app's response time
C. To automatically update the app's dependencies
D. To create user interface themes
Solution
Step 1: Understand logging purpose
Logging records events and errors during app execution.
Step 2: Connect to debugging
Tracking these events helps developers find and fix issues.
Final Answer:
To track app activity and errors for easier debugging -> Option A
Quick Check:
Logging = Track activity/errors [OK]
Hint: Logging is for tracking events and errors [OK]
Common Mistakes:
Confusing logging with performance optimization
Thinking logging updates app code automatically
Mixing logging with UI design
2. Which of the following is the correct way to set up basic logging in a FastAPI app using Python's logging module?
easy
A. log.config(level=logging.DEBUG)
B. logging.setup(level='DEBUG')
C. logging.basicConfig(level=logging.DEBUG)
D. logging.start(level=logging.DEBUG)
Solution
Step 1: Recall Python logging setup
The standard method is logging.basicConfig() to configure logging.
Step 2: Check syntax correctness
logging.basicConfig(level=logging.DEBUG) correctly sets the log level.
Final Answer:
logging.basicConfig(level=logging.DEBUG) -> Option C
Quick Check:
Use basicConfig for logging setup [OK]
Hint: Use logging.basicConfig() to configure logging [OK]