Description
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!
Activity