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

support *stacklevel* argument #103

Open
jacobbogdanov opened this issue Mar 9, 2021 · 0 comments
Open

support *stacklevel* argument #103

jacobbogdanov opened this issue Mar 9, 2021 · 0 comments

Comments

@jacobbogdanov
Copy link

In python3.8 the logging.Logger.log (and .info(), .debug(), etc...) gained a new stacklevel argument.

The stacklevel parameter is passed from code calling the debug() and other APIs. If greater than 1, the excess is used to skip stack frames before determining the values to be returned. This will generally be useful when calling logging APIs from helper/wrapper code, so that the information in the event log refers not to the helper/wrapper code, but to the code that calls it.
https://docs.python.org/3/library/logging.html#logging.Logger.findCaller

Currently coloredlogs does not respect the stacklevel argument as can be seen in this example;
lib.py

import logging


LOG = logging.getLogger(__name__)


def my_log(msg):
    LOG.info(msg, stacklevel=2)

and app.py

import logging
import sys

import coloredlogs

import lib


coloredlogs.install(logging.INFO)
# logging.basicConfig(
#     stream=sys.stdout,
#     level=logging.INFO,
#     format="%(asctime).19s %(levelname)s %(filename)s:%(lineno)s %(message)s ",
# )

lib.my_log("hello!")

running this with coloredlogs yields

$ python app.py
2021-03-09 11:33:37 dev lib[92930] INFO hello!

commenting out the coloredlogs.install(logging.INFO), and un-commenting the logging.basicConfig(...) yields

python app.py
2021-03-09 11:35:33 INFO app.py:16 hello! 
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

1 participant