FastAPI Logging

Inspired by https://stackoverflow.com/questions/63510041/adding-python-logging-to-fastapi-endpoints-hosted-on-docker-doesnt-display-api 

   

 Set up your logger configuration 

 from pydantic import BaseModel

class LogConfig(BaseModel):

 """Logging configuration to be set for the server"""

 LOGGER_NAME: str = "mycoolapp"

 LOG_FORMAT: str = "%(levelprefix)s | %(asctime)s | %(message)s"

 LOG_LEVEL: str = "DEBUG"

 # Logging config

 version = 1

 disable_existing_loggers = False

 formatters = {

 "default": {

 "()": "uvicorn.logging.DefaultFormatter",

 "fmt": LOG_FORMAT,

 "datefmt": "%Y-%m-%d %H:%M:%S",

 },

 }

 handlers = {

 "default": {

 "formatter": "default",

 "class": "logging.StreamHandler",

 "stream": "ext://sys.stderr",

 },

 }

 loggers = {

 LOGGER_NAME: {"handlers": ["default"], "level": LOG_LEVEL},

 } 

 Import the configuration and use it: 

 from logging.config import dictConfig

import logging

from .config import LogConfig

dictConfig(LogConfig().dict())

logger = logging.getLogger("mycoolapp")

logger.info("Dummy Info")

logger.error("Dummy Error")

logger.debug("Dummy Debug")

logger.warning("Dummy Warning")