27

I have a fastapi app on which I want to add python logging. I followed the basic tutorial and added this, however this doesn't add API but just gunicorn logging.

So I have a local server hosted using docker build so running server using docker-compose up and testing my endpoints using api client (Insomnia, similar to postman). Below is the code where no log file is created and hence no log statements added.

My project str is as follows:

project/
  src/ 
    api/
      models/ 
        users.py
      routers/
        users.py
      main.py
      logging.conf
"""
main.py Main is the starting point for the app.
"""
import logging
import logging.config
from fastapi import FastAPI
from msgpack_asgi import MessagePackMiddleware
import uvicorn

from api.routers import users


logger = logging.getLogger(__name__)


app = FastAPI(debug=True)
app.include_router(users.router)


@app.get("/check")
async def check():
    """Simple health check endpoint."""
    logger.info("logging from the root logger")
    return {"success": True}


Also, I am using gunicorn.conf that looks like this:

[program:gunicorn]
command=poetry run gunicorn -c /etc/gunicorn/gunicorn.conf.py foodgame_api.main:app
directory=/var/www/
autostart=true
autorestart=true
redirect_stderr=true

And gunicorn.conf.py as

import multiprocessing
bind = "unix:/tmp/gunicorn.sock"
workers = multiprocessing.cpu_count() * 2 + 1
worker_class = "uvicorn.workers.UvicornWorker"
loglevel = "debug"
errorlog = "-"
capture_output = True
chdir = "/var/www"
reload = True
reload_engine = "auto"
accesslog = "-"
access_log_format = '%(h)s %(l)s %(u)s %(t)s "%(r)s" %(s)s %(b)s "%(f)s" "%(a)s"'

This is my output terminal for the above API endpoint on docker:

enter image description here

Could anyone please guide me here? I am new to FastApi so some help will be appreciated.

Atihska
  • 4,803
  • 10
  • 56
  • 98

2 Answers2

58

Inspired by @JPG's answer, but using a pydantic model looked cleaner.

You might want to expose more variables. This config worked good for me.


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},
    }

Then import it into your main.py file as:

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")

Which gives:

enter image description here

Tobias Feil
  • 2,399
  • 3
  • 25
  • 41
Yash Nag
  • 1,096
  • 12
  • 16
19

I would use dict log config

create a logger config as below,

# my_log_conf.py

log_config = {
    "version": 1,
    "disable_existing_loggers": False,
    "formatters": {
        "default": {
            "()": "uvicorn.logging.DefaultFormatter",
            "fmt": "%(levelprefix)s %(asctime)s %(message)s",
            "datefmt": "%Y-%m-%d %H:%M:%S",

        },
    },
    "handlers": {
        "default": {
            "formatter": "default",
            "class": "logging.StreamHandler",
            "stream": "ext://sys.stderr",
        },
    },
    "loggers": {
        "foo-logger": {"handlers": ["default"], "level": "DEBUG"},
    },
}

Then, load the config using dictConfig function as,

from logging.config import dictConfig
from fastapi import FastAPI

from some.where.my_log_conf import log_config

dictConfig(log_config)

app = FastAPI(debug=True)

Note: It is recommended to call the dictConfig(...) function before the FastAPI initialization.

After the initialization, you can use logger named foo-logger anywhere in your code as,

import logging

logger = logging.getLogger('foo-logger')

logger.debug('This is test')
JPG
  • 82,442
  • 19
  • 127
  • 206
  • 1
    It doesn't work for me. I only see gunicorn request based HTTP logs and not the ones I print using logging module on path functions. – Atihska Aug 25 '20 at 00:28
  • 1
    Do you see any logs if you are running without docker? btw, this example should work as-is with both gunicorn as well as with uvicorn – JPG Aug 25 '20 at 02:09
  • 1
    BTW, I did test this solution in the Docker environment too, and it seems fine. You can test the same with this demo project of [mine, with branch **`so-63510041`**](https://github.com/jerinpetergeorge/fast-api-example/tree/so-63510041). You will find the commands in the `README.md` – JPG Aug 25 '20 at 03:00
  • is there a way to add response time to the logs? – ScipioAfricanus Feb 10 '22 at 16:47