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 adds a logging handler to the root logger #630

Closed
Ronserruya opened this issue Apr 12, 2020 · 3 comments
Closed

Uvicorn adds a logging handler to the root logger #630

Ronserruya opened this issue Apr 12, 2020 · 3 comments

Comments

@Ronserruya
Copy link

Ronserruya commented Apr 12, 2020

When using the default config, unicorn adds an handler to the root logger:
https://github.com/encode/uvicorn/blob/master/uvicorn/config.py#L86

Since all loggers in python are propagated back to the root logger, this means that all logs from all loggers will reach the handler Uvloop adds

When I create a custom logger in my app all my log messages are printed twice, once from my handlers, and then from the uvicorn handler on the root logger.

Since uvicorn always use "uvicorn.*" loggers, I was able to create a new "unicorn" logger and move the handler to it on app startup

def disable_uvicorn_root_logger():
    """ Uvicorn adds a default handler to the root logger, so all logs messages are duplicated"""
    uvicorn_logger = logging.getLogger('uvicorn')
    uvicorn_logger.addHandler(logging.root.handlers[0])
    logging.root.handlers = []

I'm not sure if its the intended functionality, it was kinda unexpected for me.

If you wish to fix it, It can be easily fixed by changing "" to "uvicorn" in
https://github.com/encode/uvicorn/blob/master/uvicorn/config.py#L86

But doing so will break the existing functionality for users that currently just use logging without any config and it works for them.

@xeor
Copy link

xeor commented Apr 16, 2020

This bit me as well in Delgan/loguru#247 trying to get all logs handled by loguru. It started with some logs from the aioredis that didnt want to get intercepted by loguru.

stchris added a commit to stchris/uvicorn that referenced this issue May 15, 2020
Overriding the root logger is probably not intended here. See discussion in encode#630
tomchristie pushed a commit that referenced this issue May 15, 2020
Overriding the root logger is probably not intended here. See discussion in #630
@pawamoy
Copy link
Sponsor Contributor

pawamoy commented Jun 3, 2020

I posted a solution to intercept everything (gunicorn/uvicorn/httpx/etc.) here, hope this helps the next ones who want to do the same thing 🙂

@euri10
Copy link
Member

euri10 commented Jul 22, 2020

closed by #674

@euri10 euri10 closed this as completed Jul 22, 2020
Devops555 pushed a commit to Devops555/uvicorn that referenced this issue Aug 7, 2021
Overriding the root logger is probably not intended here. See discussion in encode/uvicorn#630
cr313 added a commit to cr313/py-project-uvicorn that referenced this issue Apr 19, 2024
Overriding the root logger is probably not intended here. See discussion in encode/uvicorn#630
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants