Configure Python logging with dictConfig
source link: https://jdhao.github.io/2024/03/11/python-logging-dictconfig/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Configure Python logging with dictConfig
Apart from directly configuring the Python logging in the code itself. We can also configuring the logging using fileConfig or dictConfig.
The dictConfig is more powerful than fileConfig and is the recommended way to configure logging.
import json
import logging
with open("path/to/logging_conf.json") as f:
conf = json.load(f)
logging.config.dictConfig(conf)
The content of logging_conf.json
:
{
"version": 1,
"disable_existing_loggers": True,
"formatters": {
"standard":{
"format": "%(asctime)s [%(levelname)s] [%(name)s:%(lineno)d]: %(message)s"
}
},
"handlers":
{
"console":{
"level": "DEBUG",
"formatter": "standard",
"class": "logging.StreamHandler",
"stream": "ext://sys.stdout"
}
},
"loggers":{
"requests":{
"level": "WARNING"
},
"my_module":{
"level": "INFO"
}
},
"root": {
"level": "DEBUG",
"handlers": ["console"]
}
}
You can also write the logging configuration in YAML format (easier to write) and read it using the yaml package.
version: 1
disable_existing_loggers: true
formatters:
standard:
format: '%(asctime)s [%(levelname)s] [%(name)s:%(lineno)d]: %(message)s'
handlers:
console:
level: DEBUG
formatter: standard
class: logging.StreamHandler
stream: ext://sys.stdout
loggers:
requests:
level: WARNING
my_module:
level: INFO
root:
level: DEBUG
handlers: ['console']
Note that you should be very careful with disable_existing_loggers
.
If this option is set to True
, any logger that is defined before calling logging.config.dictConfig()
will be disabled.
This may cause subtle logging issues that are hard to debug.
For example, suppose you have a module my_module.py
:
import logging
logger = logging.getLogger(__name__)
# ....
def some_func():
logger.info("some message from my_module")
# ... other statements
In your entry point to this project, you have something like this:
import logging
import my_module
logging.config.dictConfig("/path/to/config/json")
my_module.some_func()
The logging message from my_module will not be printed.
You need to set disable_existing_loggers
to False
,
or you need to configure the logger for my_module.py
explicitly under the loggers
keys in the JSON config file.
Under the key loggers
, you can configure the behavior of different loggers.
Disable logging for 3rd party package/modules
To disable logging from 3rd party packages/modules, e.g., “urllib3”, we can configure their logging level in the configuration file like this:
loggers:
urllib3:
level: WARNING
Or you can also use Python code to disable the logging for a module completely:
logging.getLogger("urllib3").disabled = True
Check existing logger config?
Sometimes, we way to check the configuration for a logger to debug the logging issue.
To check the existing loggers, we can use logging.root.manager.loggerDict
.
The keys are the module names and the value are the respecitve loggers.
import logging
current_logger = logging.root.manager.loggerDict['__main__']
print(current_logger.level, current_logger.disabled)
After you get the loggers, you can check the handlers associated with the loggers. Further, you can also check the formatter that is used by a handler. In this way, you can get an idea of the configuration for the different loggers.
Note that logging.root.manager.loggerDict
does not contain the root logger.
To get the root logger, you can call logging.getLogger()
with empty argument:
root_logger = logging.getLogger()
print(root_logger.handlers[0].formatter._fmt)
References
Recommend
-
31
在 PyCon 2018 上,Mario Corchero 介绍了在开发过程中如何更方便轻松地记录日志的流程。 整个演讲的内容包括: 为什么日志记录非...
-
42
-
41
Logging is a very useful tool in a programmer’s toolbox. It can help you develop a better understanding of the flow of a program and discover scenarios that you might not even have thought of while developing. Lo...
-
78
在部署项目时,不可能直接将所有的信息都输出到控制台中,我们可以将这些信息记录到日志文件中,这样不仅方便我们查看程序运行时的情况,也可以在项目出现故障时根据运行时产生的日志快速定位问题出现的位置。 1、日志级别
-
23
Python provides more than one way to format strings: %-formatting ,
-
20
在《
-
8
日志打印之logging.config.dictConfig使用总结 By:授客 QQ:1033553122 #实践环境 WIN 10 Python 3.6.5 #函数说明 logging.config.dictConfig(config) dictConfig函数位于logging...
-
9
Tutorial How To Configure HAProxy Logging with Rsyslog on CentOS 8 [Quickstart] CentOS
-
9
Tutorial How To Configure HAProxy Logging with Rsyslog on Rocky Linux 8 [Quickstart] Logging
-
8
Quarkus uses the JBoss Log Manager project as facade for application logging. Therefore, the main configuration options should be familiar to JBoss/WildFly users. Let’s see how to configure most common options.
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK