[Python] logging.logger Outputs the caller's log correctly from the wrapper

background

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

How to output stack information

Python 3.8 and above: Use logging stacklevel

Starting with Python 3.8, the logging module has support for this use case. There are two possible ways to use it.

Pass as a keyword argument for log output

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.

Reference using the method of the logger object

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.

Python 3.7 and earlier: Define your own functions

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())

the end

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

[Python] logging.logger Outputs the caller's log correctly from the wrapper
Existence from the viewpoint of Python
Use the Flickr API from Python
Launch the Python interpreter from Git bash
From Python 3.4, pip becomes the standard installer! ??
Learning notes from the beginning of Python 2
[Python] Get the main color from the screenshot
python log
Get the contents of git diff from python
Created a Python wrapper for the Qiita API
Let's parse the git commit log in Python!
When do python functions affect the caller's arguments?
A memorandum about the Python tesseract wrapper library
Operate the schedule app using python from iphone
Use the nghttp2 Python module from Homebrew from pyenv's Python
Call Polly from the AWS SDK for Python
Try accessing the YQL API directly from Python 3
[Shell script] Calculates the elapsed time from the START and END log files and outputs it.