__Logger class __: This is the main body of logging. Manage from log generation to sending to the desired location. Handler and Filter can be set for each object generated from Logger class. Logger has a hierarchical structure (described later) and inherits the parent settings by inheritance.
__root (rootLogger object) __: An object of the default Logger class and the parent of all loggers. Must be one. All logs written in logging.info ()
etc. flow here.
__Original Logger object __: An object of your own Logger class created by getLogger ("logger name")
. By creating each file, you can explicitly see which file the log is, and you can set the log level and handler for each. It is possible to create a hierarchical structure (described later).
LogRecord: Log body. Generated by Logger.
Handler: Manage where LogRecord is delivered (standard output, file write, HTTP request, etc.). Specify in Logger. → Various handlers: https://docs.python.org/ja/3/howto/logging.html#useful-handlers
Formatter: Specify the log format. Specify in Handler.
Filter: Manage which logs are output. Specify in Logger or Handler.
Check the logging flow using the above hierarchical structure mod2
and mod2.fuga
.
https://docs.python.org/ja/3/howto/logging.html#logging-flow
loggingtest.py
import logging
import loggingtest2
logger = logging.getLogger("mod2")
logger.setLevel(logging.DEBUG)
#Format generation
formatter = logging.Formatter('parent: %(name)s [%(levelname)s] %(message)s')
#Handler generation of standard error output
handler = logging.StreamHandler()
#Various settings to the handler
handler.setLevel(logging.ERROR)
handler.setFormatter(formatter)
#Attach a handler to the logger
logger.addHandler(handler)
if __name__ == "__main__":
loggingtest2.test()
loggingtest2.py
import logging
def test():
logger = logging.getLogger("mod2.fuga")
logger.setLevel(logging.DEBUG)
#Format generation
formatter = logging.Formatter('child: %(name)s [%(levelname)s] %(message)s')
#Handler generation / setting
handler = logging.StreamHandler()
handler.setLevel(logging.WARNING)
handler.setFormatter(formatter)
logger.addHandler(handler)
#logger.propagate = False #See below
logger.error("bbb")
When executed in this state, it is output from both the parent handler and the child handler.
$ python loggingtest.py
child: mod2.fuga [ERROR] bbb
parent: mod2.fuga [ERROR] bbb
As you can see by trying other things, if you set the handler condition of loggingtest2.py to "CRITICAL" or higher, child: ~~
will not be output, and if you remove the dot of getLogger ()
and flatten the hierarchical structure Only child: ~~
is output because the handler of the parent mod2 is no longer called.
The literal translation of propagate is "propagate", and this attribute specifies whether to propagate the LogRecord to its parent.
You can confirm that the parent is not called by uncommenting logger.propagate = False
in the above code.
Settings for logging can be described in a separate file called logging.conf
and read withfileConfig ()
. As a result, the logging setting part can be separated from the processing body. See official documentation for details.
Recommended Posts