简体   繁体   中英

Python logging - how do I disable or set different logging level for some modules, using the logger configuration file?

I'm reading the logging configuration from a config file:

logging.config.fileConfig(fname=logConfigFilePath, disable_existing_loggers=False) .

The logging config is as basic as it can be:

[loggers]
keys=root

[logger_root]
level=DEBUG
handlers=consoleHandler

[handlers]
keys=consoleHandler

[handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=consoleFormatter
args=(sys.stdout,)

[formatters]
keys=consoleFormatter

[formatter_consoleFormatter]
format=%(asctime)s %(levelname)s %(filename)s-%(funcName)s %(message)s

I need to set a different logging level (eg. WARNING ) for some specific modules, like this one:

DEBUG connectionpool.py-_new_conn Starting new HTTPS connection (1):...

How can I do this, NOT programmatically, but using the config file ?

Thanks.

In the configuration, you can manipulate the runtime logger through its qualname .

For example let's say your logger is in the module ab and you're using logging.getLogger(__name__) .

You'd extend your loggers section to contain the new logger configuration name, and a new section for the logger where you give it the different level, and link it to the runtime logger through the qualname

[loggers]
keys=root,changed_logger

[logger_changed_logger]
qualname=a.b
level=DEBUG
;inherit all handlers from parent, do not create any new ones
propagate=1
handlers=

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