--As you extend logging.logger, you may want to create a LoggerWrapper object that has a Logger object in its field.
--However, in that case, the caller information included in the LogRecord object = stack information (pathname
, lineno
, func
) refers to the log method of LoggerWrapper, and the original caller information is output. It will not be done.
--Therefore, it is necessary to output the stack information correctly by the following method.
--The following method changes the depth of the referenced stack.
Starting with Python 3.8, the logging module has support for this use case. There are two possible ways to use it.
https://docs.python.org/ja/3/library/logging.html#logging.Logger.debug
logger.info(msg, stacklevel=2)
stacklevel
is an added parameter. Set the depth to the caller you want to refer to.
https://docs.python.org/ja/3/library/logging.html#logging.Logger.findCaller
stack_filename, stack_lineno, stack_function = logger.findCaller(True, 3)[0:3]
--Pass the depth as the second argument of findCaller. --If you want to collect stack information in LoggerAdapter or LoggerFormatter, it seems better to use this.
There is no support in the logging package, so use the inspect
package to define a function to get stack information.
[2]Represents the depth
import inspect
def stack_info() -> dict:
stack = inspect.stack()[2]
return {
'stack_filename': stack.filename,
'stack_lineno': stack.lineno,
'stack_function': stack.function,
}
Usage example
logger.info(msg, extra=stack_info())
--When using LoggerWrapper object, it is necessary to prepare in order to output stack information correctly like this time. ――Therefore, it seems better to consider introducing the LoggerWrapper object after considering whether it can be done by using logging.LoggerAdapter etc.
Recommended Posts