在我的python json配置文件中,我有以下格式化程序,我保留了一些自定义日志字段,如applicationName和applicationState
{
"version": 1,
"disable_existing_loggers": true,
"formatters": {
"custom": {
"format": "%(asctime)s %(levelname)s [%(applicationName)s,%(applicationState)s] %(message)s"
}
},
"handlers": {
"console": {
"class": "logging.StreamHandler",
"level": "INFO",
"formatter": "custom"
}
},
"root": {
"level": "INFO",
"handlers": ["console"]
},
"loggers": {
"myCustomLogger": {
"level": "INFO",
"handlers": ["console"],
"propagate": "false"
}
}
}
然后我希望像这样的日志语句提供这些额外内容
logger.info("this is my log message", extra={'applicationName': 'myapp', 'applicationStatue': 'Stable'})
当我在json配置中保存这些并在我的python代码中加载它时,我在applicationName和applicationState上遇到一个关键错误
with open(path, 'rt') as f:
config = json.load(f)
logging.config.dictConfig(config)
我得到的错误是:
Traceback (most recent call last):
File "C:\Python27\lib\logging\__init__.py", line 861, in emit
msg = self.format(record)
File "C:\Python27\lib\logging\__init__.py", line 734, in format
return fmt.format(record)
File "C:\Python27\lib\logging\__init__.py", line 469, in format
s = self._fmt % record.__dict__
KeyError: u'applicationName'
Logged from file _internal.py, line 87
然而,当我在python代码本身中创建这个格式化程序时,如下所示,它工作正常,我也能够在log extra中传递applicationName和applicationState,如前所述
formatter = logging.Formatter('%(asctime)s %(levelname)s [%(applicationName)s,%(applicationState)s] %(message)s')
handler.setFormatter(formatter)
有谁知道如何在json配置中使用它?
编辑:
额外信息 我让记录器记录这样的东西:
logging.getLogger( “MyApp的”)