question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Duplicate logs when use logging.basicConfig in application

See original GitHub issue

Checklist

  • The bug is reproducible against the latest release or master.
  • There are no similar issues or pull requests to fix it yet.

Describe the bug

When use logging.basicConfig to config logging in application, uvicorn logs will duplicate in console.

Steps to reproduce the bug

app.py:

import logging
from fastapi import FastAPI

logging.basicConfig(level=logging.INFO)

app = FastAPI(debug=True)

run it:

uvicorn app:app

Expected behavior

expect no duplicate logs.

Actual behavior

uvicorn.error logs duplicated:

INFO:     Started server process [85610]
INFO:uvicorn.error:Started server process [85610]
INFO:     Waiting for application startup.
INFO:uvicorn.error:Waiting for application startup.
INFO:     Application startup complete.
INFO:uvicorn.error:Application startup complete.
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:uvicorn.error:Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)

Debugging material

I think the uvicorn logger should not propagate, because it already handled by uvicorn handler. And the behavior of gunicorn is no duplicate logs.

Add "propagate": False to https://github.com/encode/uvicorn/blob/master/uvicorn/config.py#L107 can fix the issue.

"uvicorn": {"handlers": ["default"], "level": "INFO", "propagate": False},

Environment

Running uvicorn 0.15.0 with CPython 3.9.7 on Darwin

Additional context

Maybe related: https://github.com/encode/uvicorn/issues/614

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:3
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
yuval-illumexcommented, Jul 5, 2022

Any Update on this one? It’s really annoying 😦

3reactions
bachyacommented, Jul 29, 2022

@matthew-viglione My workaround in the meantime has been:

uvicorn_logger = logging.getLogger("uvicorn")
uvicorn_logger.removeHandler(uvicorn_logger.handlers[0])
Read more comments on GitHub >

github_iconTop Results From Across the Web

Duplicate logs when calling logging.basicConfig
Problem: When I call logging.basicConfig in addition to configuring my own logger, I get duplicate logs · I'm running python 3.6.8 · Easy...
Read more >
Logging HOWTO — Python 3.11.1 documentation
To log variable data, use a format string for the event description message and append the variable data as arguments. For example: import...
Read more >
A Step-by-Step Guide to Python Logging
A comprehensive guide to understanding the advantages and usage of Python Logging module.
Read more >
Python Logging Basics - The Ultimate Guide To Logging
The module provides a way for applications to configure different log handlers and a ... a utility function called basicConfig that handles most...
Read more >
Python Logging Guide - Best Practices and Hands-on ...
Logging with Django and uWSGI: To deploy web applications you can use StreamHandler as logger which sends all logs to For Django you...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found