Challenge - 5 Problems
FastAPI Logging Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2: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"}
Attempts:
2 left
💡 Hint
Think about the logging level used in the logger.error call.
✗ Incorrect
The logger.error method logs messages with ERROR level. By default, Uvicorn's error logger prints ERROR level messages to the console.
❓ Configuration
intermediate2: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?
Attempts:
2 left
💡 Hint
Check the parameter names and logging level types.
✗ Incorrect
The filename parameter sets the log file, level expects a logging level constant, and format defines the log message format.
❓ Troubleshoot
advanced2: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?
Attempts:
2 left
💡 Hint
Check the logging level configuration in your app or server.
✗ Incorrect
If the logging level is set to CRITICAL, for example, ERROR messages will not show. The logger.error method does print messages if the level allows.
🔀 Workflow
advanced2: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?
Attempts:
2 left
💡 Hint
Think about a centralized way to handle all exceptions.
✗ Incorrect
A global exception handler catches all exceptions, logs them, and returns a response. Wrapping every route is tedious and error-prone.
✅ Best Practice
expert3: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?
Attempts:
2 left
💡 Hint
Consider security and privacy best practices for error information.
✗ Incorrect
Detailed error logs should be kept internally for developers, while clients receive safe, generic messages to avoid exposing sensitive info.