Logging helps you see what your FastAPI app is doing. It records messages about events, errors, and info so you can understand and fix problems.
Logging configuration in FastAPI
Introduction
Syntax
FastAPI
import logging logging.basicConfig(level=logging.LEVEL, format='FORMAT') logger = logging.getLogger('your_logger_name') logger.LEVEL('Your message')
Replace LEVEL with logging levels like DEBUG, INFO, WARNING, ERROR, or CRITICAL.
The format controls how the log message looks, including time, level, and message.
Examples
INFO:App started.FastAPI
import logging logging.basicConfig(level=logging.INFO, format='%(levelname)s:%(message)s') logger = logging.getLogger('app') logger.info('App started')
FastAPI
import logging logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s') logger = logging.getLogger('app') logger.debug('Debugging info')
Sample Program
This FastAPI app logs an info message every time the root endpoint is called.
FastAPI
import logging from fastapi import FastAPI logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') logger = logging.getLogger('fastapi_app') app = FastAPI() @app.get('/') async def root(): logger.info('Root endpoint was called') return {'message': 'Hello World'}
Important Notes
Use different log levels to control what messages appear.
You can configure logging to write to files instead of the console.
Logging helps you find problems faster than print statements.
Summary
Logging records important events in your FastAPI app.
Use logging.basicConfig to set up logging format and level.
Call logger methods like info() or debug() to write messages.
Practice
1. What is the main purpose of configuring logging in a FastAPI application?
easy
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 AQuick 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
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 CQuick Check:
Use basicConfig for logging setup [OK]
Hint: Use logging.basicConfig() to configure logging [OK]
Common Mistakes:
- Using non-existent logging.setup() method
- Misspelling logging as log
- Using start() instead of basicConfig()
3. Given this FastAPI logging setup code:
What will be printed to the console?
import logging
logging.basicConfig(level=logging.WARNING)
logging.debug('Debug message')
logging.warning('Warning message')What will be printed to the console?
medium
Solution
Step 1: Understand logging levels
Level WARNING means only WARNING and above are shown, DEBUG is lower level.Step 2: Check which messages print
DEBUG message is ignored, WARNING message is printed.Final Answer:
Warning message -> Option AQuick Check:
Level WARNING shows warnings only [OK]
Hint: Only messages at or above set level print [OK]
Common Mistakes:
- Expecting DEBUG messages to print at WARNING level
- Thinking all messages print regardless of level
- Confusing logging levels order
4. You wrote this code to configure logging:
But no output appears. What is the likely problem?
import logging
logging.basicConfig(level='DEBUG')
logging.info('Info message')But no output appears. What is the likely problem?
medium
Solution
Step 1: Check logging level argument type
logging.basicConfig expects level as a constant like logging.DEBUG, not a string.Step 2: Understand effect of wrong level
Passing 'DEBUG' string is ignored, default level WARNING applies, so info messages don't show.Final Answer:
The level argument should be logging.DEBUG, not a string -> Option BQuick Check:
Level must be logging.DEBUG constant [OK]
Hint: Use logging.DEBUG constant, not string 'DEBUG' [OK]
Common Mistakes:
- Passing level as string instead of constant
- Calling basicConfig after logging calls
- Assuming info messages never print
5. You want to log messages to a file named
app.log with level INFO and include timestamps. Which is the correct logging configuration code?hard
Solution
Step 1: Check correct parameter names
logging.basicConfig uses filename, level, and format parameters correctly.Step 2: Verify format string and level
Format '%(asctime)s - %(message)s' adds timestamp; level INFO matches requirement.Final Answer:
logging.basicConfig(filename='app.log', level=logging.INFO, format='%(asctime)s - %(message)s') -> Option DQuick Check:
Use filename, level=logging.INFO, correct format [OK]
Hint: Use filename and format with %(asctime)s for timestamps [OK]
Common Mistakes:
- Using wrong parameter names like file instead of filename
- Passing level as string instead of constant
- Incorrect format placeholders
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
