Challenge - 5 Problems
Logging Mastery in FastAPI
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate1:30remaining
Output of logging configuration with custom format
Given this FastAPI logging setup, what will be the output when a GET request is made to '/'?
FastAPI
import logging from fastapi import FastAPI app = FastAPI() logging.basicConfig(level=logging.INFO, format='%(levelname)s:%(message)s') logger = logging.getLogger(__name__) @app.get('/') async def root(): logger.info('Root endpoint accessed') return {'message': 'Hello World'}
Attempts:
2 left
💡 Hint
Check the logging format string and case sensitivity.
✗ Incorrect
The logging format specifies level name in uppercase followed by a colon and the message. The message is exactly 'Root endpoint accessed' with capital R.
❓ Configuration
intermediate2:00remaining
Correct YAML logging config for rotating file handler
Which YAML snippet correctly configures a rotating file handler for FastAPI logs with max 5 files and 1MB each?
Attempts:
2 left
💡 Hint
RotatingFileHandler requires maxBytes and backupCount for size-based rotation.
✗ Incorrect
Option A correctly uses RotatingFileHandler with maxBytes 1MB (1048576 bytes) and backupCount 5. Others either use wrong handler or wrong sizes.
❓ Troubleshoot
advanced2:00remaining
Why are logs missing in production with this FastAPI setup?
You configured logging in FastAPI with this code but see no logs in production. What is the most likely cause?
import logging
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger('uvicorn.error')
logger.info('App started')
Attempts:
2 left
💡 Hint
Consider how uvicorn manages logging internally.
✗ Incorrect
Uvicorn configures logging itself when started, so calling basicConfig in app code is ignored. Logs must be configured via uvicorn or external config.
🔀 Workflow
advanced2:30remaining
Order steps to enable JSON logging in FastAPI with uvicorn
Arrange these steps in the correct order to enable JSON formatted logs in a FastAPI app running with uvicorn.
Attempts:
2 left
💡 Hint
Think about installing dependencies before using them.
✗ Incorrect
First install the package, then create the formatter, configure uvicorn to use it, and finally run uvicorn with that config.
✅ Best Practice
expert2:00remaining
Which logging practice improves FastAPI app observability in production?
Select the best practice that improves observability and troubleshooting in a production FastAPI app.
Attempts:
2 left
💡 Hint
Think about how logs help in tracing and analyzing issues.
✗ Incorrect
Structured JSON logs with request IDs and timestamps help correlate events and improve observability. Other options reduce usefulness or cause issues.