Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

uvicorn - access log format is not working #2299

Closed
meenraj opened this issue Apr 3, 2020 · 8 comments
Closed

uvicorn - access log format is not working #2299

meenraj opened this issue Apr 3, 2020 · 8 comments

Comments

@meenraj
Copy link

meenraj commented Apr 3, 2020

the access log format below is not getting printed - and looks like uvicorn worker class is overriding the access log format

%(h)s %(l)s %(u)s %(t)s "%(r)s" %(s)s %(b)s %(D)s %({header}i)s

@lucifer-wsp
Copy link

I have the same issue, the log format not worked.

uvicorn = "0.13.4"
gunicorn = "^20.1.0"
django = "3.1.1"

and gunicorn.config.py :

access_log_format = '[host: %h|pid: %p|timestamp: %t](%H)s (%r)s (%m)s (%U)s referer (%f)s => response %(s)s %(b)s bytes %(D)s'

finally, i use the uvicorn access log and redirect stdout/err into gunicorn error log.

i config the settings like this to use uvicorn access log:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'uvicorn': {
            'format': '[%(asctime)s %(process)s] [%(levelname)s] %(message)s response time %(msecs)03d',  # NOQA
            'datefmt': "%d/%b/%Y %H:%M:%S",
        },
    },
    'handlers': {
        'uvicorn': {
            'level': 'DEBUG',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': UVICORN_LOG,
            'formatter': 'uvicorn',
        },
    },
    'loggers': {
        'uvicorn.access': {
            'handlers': ['console', 'uvicorn'],
            'level': 'DEBUG',
            'propagate': False,
        },
   },
},

and set error log and capture output in gunicorn.config.py.

errorlog = '/myproject/logs/gunicorn.log'
capture_output = True

Someone has a better idea ? Because i don't think that's a good way.

@Kludex
Copy link

Kludex commented Aug 27, 2021

uvicorn doesn't honor gunicorn's access_log_format yet.

We are working on it.

@pythonwood
Copy link

uvicorn doesn't honor gunicorn's access_log_format yet.

We are working on it.

@Kludex thank you !
what is the plan now ?

@Kludex
Copy link

Kludex commented Mar 31, 2023

There's nothing that Gunicorn can do, this should be solved on uvicorn's side.

You can follow this on: encode/uvicorn#527

Feel free to close this issue @benoitc 👀

@suhcrates-web
Copy link

I got the answer.
see that gunicorn anyway writes log records, and it follows certain default format. And such default format is configured in somwhere. Then, you can modify that config.
then, where is the default config? it is in the /sitepackages/gunicorn/glogging.py where the gunicorn is installed. it is the 'access_fmt' variable in the 'Logger' class.

@tilgovi
Copy link
Collaborator

tilgovi commented Dec 28, 2023

Closing as this appears to be a downstream issue with uvicorn.

@tilgovi tilgovi closed this as completed Dec 28, 2023
@waketzheng
Copy link

For people who using FastAPI, one solution is like this:

import logging
from contextlib import asynccontextmanager
from fastapi import FastAPI

LOG_FMT = "%(asctime)s - %(levelname)s - %(message)s"

def config_access_log_to_show_time():
    logger = logging.getLogger("uvicorn.access")
    handler = logging.StreamHandler()
    handler.setFormatter(logging.Formatter(LOG_FMT))
    logger.addHandler(handler)

@asynccontextmanager
async def lifespan(app: FastAPI):
    config_access_log_to_show_time()
    yield

app = FastAPI(lifespan=lifespan)

@bolee
Copy link

bolee commented Mar 29, 2024

For people who using FastAPI, one solution is like this:

import logging
from contextlib import asynccontextmanager
from fastapi import FastAPI

LOG_FMT = "%(asctime)s - %(levelname)s - %(message)s"

def config_access_log_to_show_time():
    logger = logging.getLogger("uvicorn.access")
    handler = logging.StreamHandler()
    handler.setFormatter(logging.Formatter(LOG_FMT))
    logger.addHandler(handler)

@asynccontextmanager
async def lifespan(app: FastAPI):
    config_access_log_to_show_time()
    yield

app = FastAPI(lifespan=lifespan)

only for uvicorn ,but not woring gunicorn config.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants