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

Duplicate logs when use logging.basicConfig in application #1285

Closed
2 tasks done
guyskk opened this issue Dec 8, 2021 · 8 comments · Fixed by #1288
Closed
2 tasks done

Duplicate logs when use logging.basicConfig in application #1285

guyskk opened this issue Dec 8, 2021 · 8 comments · Fixed by #1288

Comments

@guyskk
Copy link
Contributor

guyskk commented Dec 8, 2021

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: #614

@euri10
Copy link
Member

euri10 commented Dec 8, 2021

yes sounds sensible, fancy throwing a PR @guyskk ?

@guyskk
Copy link
Contributor Author

guyskk commented Dec 9, 2021

@euri10 👌 here is the PR: #1288

@gretkierewicz
Copy link

...

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.
...

Adding propagate = False is not a fix. It turs off all uvicorn's logs.

@yuval-illumex
Copy link

Any Update on this one? It's really annoying :(

@matthew-viglione
Copy link

Has anyone identified a useful workaround in the meantime?

@guyskk
Copy link
Contributor Author

guyskk commented Jul 6, 2022

Adding propagate = False is not a fix. It turs off all uvicorn's logs.

@gretkierewicz That's not correct, propagate = False just stop propagate logs to ancestors, the log message still handled by uvicorn log handers.

Python Docs also note that If you attach a handler to a logger and one or more of its ancestors, it may emit the same record multiple times. :

https://docs.python.org/3/library/logging.html#logging.Logger.propagate

propagate
If this attribute evaluates to true, events logged to this logger will be passed to the handlers of higher level (ancestor) loggers, in addition to any handlers attached to this logger. Messages are passed directly to the ancestor loggers’ handlers - neither the level nor filters of the ancestor loggers in question are considered.

If this evaluates to false, logging messages are not passed to the handlers of ancestor loggers.

The constructor sets this attribute to True.

Note
If you attach a handler to a logger and one or more of its ancestors, it may emit the same record multiple times. In general, you should not need to attach a handler to more than one logger - if you just attach it to the appropriate logger which is highest in the logger hierarchy, then it will see all events logged by all descendant loggers, provided that their propagate setting is left set to True. A common scenario is to attach handlers only to the root logger, and to let propagation take care of the rest.

@Kludex Kludex added this to the Version 0.19.0 milestone Jul 6, 2022
@bachya
Copy link

bachya commented Jul 29, 2022

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

uvicorn_logger = logging.getLogger("uvicorn")
uvicorn_logger.removeHandler(uvicorn_logger.handlers[0])

@Kludex
Copy link
Sponsor Member

Kludex commented Oct 8, 2022

eladgunders added a commit to eladgunders/fastapi-todos that referenced this issue May 12, 2023
eladgunders added a commit to eladgunders/fastapi-todos that referenced this issue May 13, 2023
* 🎨 refactored project name to config

* ✨ added smtp variables to config

* ✨ added email template for reset password

* ✨ added emails and jinja2

* upgraded uvicorn version because of logging bug - encode/uvicorn#1285

* 🎨 refactored utils folder structure

* ✨ handling forget password bt sending an email

* 💄 cosmetic

* 🔥 removed unused code

* ✨ handling account verification by sending an email

* 🔥 removed unused parameters

* added logs

* renamed function

* import utils from its __init__.py

* added reset_password_token_lifetime_seconds and verification_token_secret to config

* added the reset password and user verification lifetime to the email templates

* added validator for EMAILS_FROM_NAME

* modified docker-compose env variables

* added env variables section in README.md

* fixed mypy

* fixed bandit

* added required env variables to ci
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

Successfully merging a pull request may close this issue.

7 participants