问题描述
我正在尝试创建一个json
配置文件,并使用coloredlogs
库为logging.config.dictConfig()
加载彩色输出.
I am trying to create a json
configuration file to load with logging.config.dictConfig()
using the coloredlogs
library for a colored output.
我遇到以下错误:
Traceback (most recent call last):
File "C:\Program Files\Python36\lib\logging\config.py", line 538, in configure
formatters[name])
File "C:\Program Files\Python36\lib\logging\config.py", line 669, in configure_formatter
result = c(fmt, dfmt, style)
File "C:\Program Files\Python36\lib\site-packages\coloredlogs\__init__.py", line 834, in __init__
self.level_styles = self.nn.normalize_keys(DEFAULT_LEVEL_STYLES if level_styles is None else level_styles)
File "C:\Program Files\Python36\lib\site-packages\coloredlogs\__init__.py", line 1111, in normalize_keys
return dict((self.normalize_name(k), v) for k, v in value.items())
AttributeError: 'str' object has no attribute 'items'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File ".\sci.py", line 205, in <module>
main()
File ".\sci.py", line 180, in main
logging.config.dictConfig(json.load(json_config))
File "C:\Program Files\Python36\lib\logging\config.py", line 795, in dictConfig
dictConfigClass(config).configure()
File "C:\Program Files\Python36\lib\logging\config.py", line 541, in configure
'formatter %r: %s' % (name, e))
ValueError: Unable to configure formatter 'colored': 'str' object has no attribute 'items'
我的配置文件如下:
{
"version": 1,
"disable_existing_loggers": true,
"formatters": {
"colored": {
"class": "coloredlogs.ColoredFormatter",
"datefmt": "%H:%M:%S",
"format": "%(asctime)s %(module)-16s: %(levelname)-8s %(message)s"
}
},
"handlers": {
"console": {
"class": "logging.StreamHandler",
"formatter": "colored",
"level": "DEBUG",
"stream": "ext://sys.stdout"
}
},
"loggers": {
},
"root": {
"handlers": [
"console"
],
"level": "DEBUG"
}
}
实际上不存在有关将coloredlogs
与logging.config.dictConfig()
一起使用的文档.
The documentation about the use of coloredlogs
with logging.config.dictConfig()
is literally not existent.
推荐答案
在安装 coloredlogs
模块之后与
After installing the coloredlogs
module with
$ pip install coloredlogs
您可以将控制台输出配置为彩色.通常,这是通过类似
you are able to configure your console output to be colored. Usually this is done by have something like
coloredlogs.install(level='DEBUG', logger=logger)
在您的代码中.
但是,如果您想同时使用 coloredlogs和字典配置您需要创建一个像这样的特定格式化程序:
However, if you want to use coloredlogs together with the dictionary configurationyou need to create a specific formatter like this:
import logging.config
logging.config.dictConfig(my_logging_dict)
logger = logging.getLogger(__name__)
my_logging_dict = {
'version': 1,
'disable_existing_loggers': True, # set True to suppress existing loggers from other modules
'loggers': {
'': {
'level': 'DEBUG',
'handlers': ['console', 'file'],
},
},
'formatters': {
'colored_console': {
'()': 'coloredlogs.ColoredFormatter',
'format': "%(asctime)s - %(name)s - %(levelname)s - %(message)s",
'datefmt': '%H:%M:%S'
},
'format_for_file': {
'format': "%(asctime)s :: %(levelname)s :: %(funcName)s in %(filename)s (l:%(lineno)d) :: %(message)s",
'datefmt': '%Y-%m-%d %H:%M:%S'
},
'handlers': {
'console': {
'level': 'INFO',
'class': 'logging.StreamHandler',
'formatter': 'colored_console',
'stream': 'ext://sys.stdout'
},
'file': {
'level': 'DEBUG',
'class': 'logging.handlers.RotatingFileHandler',
'formatter': 'format_for_file',
'filename': log_file,
'maxBytes': 500000,
'backupCount': 5
}
},
}
记录文档.请注意,上面的代码片段只是我正在使用的配置的一个示例.
How to create user defined objects for logging formatters is described in the logging docu. Note that the above code snippet is just an example of the configuration I am using.
这篇关于在coloredlogs中使用日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!