Skip to content

Uvicorn adds a logging handler to the root logger #630

@Ronserruya

Description

@Ronserruya

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions