0
0
FastAPIframework~10 mins

Logging errors in FastAPI - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the logging module.

FastAPI
import [1]
Drag options to blanks, or click blank then click option'
Ajson
Bsys
Cos
Dlogging
Attempts:
3 left
💡 Hint
Common Mistakes
Importing unrelated modules like sys or os instead of logging.
2fill in blank
medium

Complete the code to create a logger instance named 'app_logger'.

FastAPI
app_logger = logging.[1]('app_logger')
Drag options to blanks, or click blank then click option'
ALogger
BgetLogger
CcreateLogger
DnewLogger
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-existent methods like createLogger or newLogger.
3fill in blank
hard

Fix the error in the code to log an error message.

FastAPI
app_logger.[1]('An error occurred')
Drag options to blanks, or click blank then click option'
Ainfo
Bwarn
Cerror
Ddebug
Attempts:
3 left
💡 Hint
Common Mistakes
Using warn, info, or debug instead of error.
4fill in blank
hard

Fill both blanks to configure logging to show messages of level ERROR or higher with a simple format.

FastAPI
logging.basicConfig(level=[1], format='[2]')
Drag options to blanks, or click blank then click option'
Alogging.ERROR
Blogging.INFO
C%(levelname)s: %(message)s
D%(message)s
Attempts:
3 left
💡 Hint
Common Mistakes
Using INFO level will show too many messages.
Using a format without level name hides severity.
5fill in blank
hard

Fill all three blanks to log an error with exception info inside a FastAPI exception handler.

FastAPI
from fastapi import Request, HTTPException

@app.exception_handler(HTTPException)
async def http_exception_handler(request: Request, exc: HTTPException):
    app_logger.[1]('HTTP error occurred: %s', exc.detail, exc_info=[2])
    return JSONResponse(status_code=exc.status_code, content=[3])
Drag options to blanks, or click blank then click option'
Aerror
BTrue
C'error': exc.detail
D'message': exc.detail
Attempts:
3 left
💡 Hint
Common Mistakes
Using exc_info=False hides stack trace.
Using wrong keys in JSON response content.