0
0
FastAPIframework~20 mins

Logging errors in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
FastAPI Logging Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output when logging an error with FastAPI's logger?
Given this FastAPI code snippet, what will be printed in the console when a GET request to '/' triggers an error log?

import logging
from fastapi import FastAPI

app = FastAPI()
logger = logging.getLogger("uvicorn.error")

@app.get("/")
def read_root():
    try:
        1 / 0
    except ZeroDivisionError as e:
        logger.error(f"Error occurred: {e}")
        return {"message": "Error logged"}
FastAPI
import logging
from fastapi import FastAPI

app = FastAPI()
logger = logging.getLogger("uvicorn.error")

@app.get("/")
def read_root():
    try:
        1 / 0
    except ZeroDivisionError as e:
        logger.error(f"Error occurred: {e}")
        return {"message": "Error logged"}
AERROR: Error occurred: division by zero
BWARNING: Error occurred: division by zero
CINFO: Error occurred: division by zero
DNo output is printed because logging is disabled by default
Attempts:
2 left
💡 Hint
Think about the logging level used in the logger.error call.
Configuration
intermediate
2:00remaining
How to configure FastAPI logging to write errors to a file?
Which logging configuration snippet correctly sets up FastAPI to write error logs to a file named 'error.log' with level ERROR?
Alogging.basicConfig(filename='error.log', level='ERROR', format='%(levelname)s:%(message)s')
Blogging.basicConfig(filename='error.log', level=logging.DEBUG, format='%(message)s')
Clogging.basicConfig(filename='error.log', level=logging.ERROR, format='%(levelname)s:%(message)s')
Dlogging.basicConfig(file='error.log', level=logging.ERROR, format='%(levelname)s:%(message)s')
Attempts:
2 left
💡 Hint
Check the parameter names and logging level types.
Troubleshoot
advanced
2:00remaining
Why are error logs not appearing in FastAPI when using logger.error?
You added logger.error("An error happened") in your FastAPI app, but no error messages appear in the console or log files. What is the most likely cause?
AThe error message is printed only if an exception is raised, not manually logged
BThe logger.error method does not print messages, only logger.warning does
CFastAPI disables logging by default and requires manual enabling
DThe logging level is set higher than ERROR, so error messages are ignored
Attempts:
2 left
💡 Hint
Check the logging level configuration in your app or server.
🔀 Workflow
advanced
2:00remaining
What is the correct workflow to capture and log exceptions globally in FastAPI?
Which approach correctly logs all unhandled exceptions globally in a FastAPI app?
AUse a custom exception handler with @app.exception_handler(Exception) that logs the error and returns a response
BSet logging.basicConfig(level=logging.ERROR) and rely on FastAPI to log automatically
CWrap every route handler in try-except blocks and log errors inside each
DUse middleware to catch exceptions but do not log them, only return error responses
Attempts:
2 left
💡 Hint
Think about a centralized way to handle all exceptions.
Best Practice
expert
3:00remaining
Which is the best practice for logging sensitive error details in FastAPI production apps?
In a production FastAPI app, how should you handle logging of sensitive error details to avoid security risks?
AAlways log full error details and stack traces to the client response for easier debugging
BLog detailed error stack traces only to secure internal log files, and return generic error messages to clients
CDisable all error logging in production to prevent any sensitive data exposure
DLog error details only in client responses and not in any log files
Attempts:
2 left
💡 Hint
Consider security and privacy best practices for error information.