简体   繁体   中英

Python - how do I configure a child logger via a logging configuration file

I have a Python package that contains a series of modules, each of which contains a library of routines with a central focus (ie one module may have text manipulation related routines). I would like to be able to configure logging for the entire package, but give the user the ability to customize logging for a given module.

I've tried to accomplish this using parent/child loggers so that each module didn't have to have more than 1 logger. I can use the parent/child approach successfully if I configure the child loggers in the module itself, but I cannot find the correct syntax for configuring child loggers via the logger configuration file. When the Python logging utility reads in the configuration file, it chokes on any logger 'keys' containing dot notation (ie if I try to define loggers "root, parent, parent.child" the ingest of the configuration file will choke on the "parent.child" key).

So, I'm looking for a solution whereby a given module only has to have a single logger, but the user can configurr logging for the entire package one way, but a different way for a specific module. The parent/child approach seemed the most promising.

If there is a way for the module to determine what loggers the logging manager has definitions for, I could use that also. For example, lets say I could define two loggers in my logging configure file named allUtil and textUtil. If the text utility module could determine if the logging manager had a textUtil logger defined, it could use that one, other wise it would use the allUtil logger. Unfortunately I have not been able to figure out the correct Python code to do this either.

I am open to suggestions.

Here is what I came up with that appears to work.

Setup:

  • I have an application called 'myapp'
  • I have a package it uses called 'utils'
  • I have a module in package 'utils' called 'miscUtils'

In the log configuration file I have the following (only pertinent content shown)

[loggers]
keys=root,myapp,utils,miscUtils
.
.
.
[logger_root]
level=DEBUG
handlers=consoleHandler

[logger_myapp]
level=CRITICAL
handlers=consoleHandler
qualname=myapplogger
propagate=0

[logger_utils]
level=WARNING
handlers=consoleHandler
qualname=myapplogger.utils
propagate=1

[logger_miscUtils]
level=DEBUG
handlers=consoleHandler
qualname=myapplogger.utils
propagate=1
.
.
.
  • In the module utils.miscUtils.py I use the logger 'myapplogger.utils.misc'.
  • In all other modules in the 'utils' package they use loggers 'myapplogger.utils.xxx' with xxx representing a particular module.
  • In the main program module myapp.py I use the logger 'myapplogger', but I explicitly set its level to INFO.

When I run the program, log messages from the main module issue logs messages from INFO on down. All 'utils' modules but miscUtils.py, will log messages from WARNING on down. However, all log messages generated from the module miscUtils.py, which uses the logger 'myapplogger.utils.miscUtils' issues log messages from DEBUG on down.

Via the configuration file I can now define overall program log levels, package log levels, and module log levels, as needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM