0
0
FastAPIframework~20 mins

Logging configuration in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Logging Mastery in FastAPI
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
1: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'}
AINFO:root endpoint accessed
BINFO:Root endpoint accessed
Croot endpoint accessed
DLogging not configured
Attempts:
2 left
💡 Hint
Check the logging format string and case sensitivity.
Configuration
intermediate
2: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?
A
version: 1
handlers:
  file:
    class: logging.handlers.RotatingFileHandler
    filename: app.log
    maxBytes: 1048576
    backupCount: 5
    level: INFO
loggers:
  uvicorn:
    handlers: [file]
    level: INFO
B
version: 1
handlers:
  file:
    class: logging.FileHandler
    filename: app.log
    maxBytes: 1048576
    backupCount: 5
    level: INFO
loggers:
  uvicorn:
    handlers: [file]
    level: INFO
C
version: 1
handlers:
  file:
    class: logging.handlers.TimedRotatingFileHandler
    filename: app.log
    when: 'midnight'
    backupCount: 5
    level: INFO
loggers:
  uvicorn:
    handlers: [file]
    level: INFO
D
version: 1
handlers:
  file:
    class: logging.handlers.RotatingFileHandler
    filename: app.log
    maxBytes: 512000
    backupCount: 10
    level: INFO
loggers:
  uvicorn:
    handlers: [file]
    level: INFO
Attempts:
2 left
💡 Hint
RotatingFileHandler requires maxBytes and backupCount for size-based rotation.
Troubleshoot
advanced
2: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')
AMissing call to logger.setLevel() to enable logging
BLogging level DEBUG is too low to show info messages
CLogger name 'uvicorn.error' does not exist, so no logs are captured
DUvicorn overrides logging config; basicConfig is ignored when running with uvicorn server
Attempts:
2 left
💡 Hint
Consider how uvicorn manages logging internally.
🔀 Workflow
advanced
2: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.
A1,2,3,4
B1,3,2,4
C2,1,3,4
D3,1,2,4
Attempts:
2 left
💡 Hint
Think about installing dependencies before using them.
Best Practice
expert
2:00remaining
Which logging practice improves FastAPI app observability in production?
Select the best practice that improves observability and troubleshooting in a production FastAPI app.
ALog only error messages to reduce log volume
BLog all debug messages to a single file without rotation
CUse structured logging with JSON format including request IDs and timestamps
DDisable logging to improve performance
Attempts:
2 left
💡 Hint
Think about how logs help in tracing and analyzing issues.