大家好,我有一个json程序,它将以json格式提供日志文件,但是我无法获取输出...这是一个名为“ logging.json”的json文件
{
"version": 1,
"disable_existing_loggers": false,
"formatters": {
"json": {
"format": "%(asctime)s - %(name)s - %(levelname)s - %(message)s",
"()": "pythonjsonlogger.jsonlogger.JsonFormatter"
}
},
"handlers": {
"console": {
"class": "logging.StreamHandler",
"formatter": "json"
}
},
"file_handler": {
"class": "logging.handlers.RotatingFileHandler",
"level": "INFO",
"formatter": "json",
"filename": "Data-Generation.log",
"mode": "a",
"encoding": "utf8"
},
"root": {
"level": "DEBUG",
"handlers": ["console", "file_handler"]
}
}
现在我正在尝试像这样在python程序中访问:
fp = open('logging.json')
logging.config.dictConfig(json.load(fp))
logger = logging.getLogger(__name__)
fp.close()
logger.info("Logging is started")
我遇到此错误:
Traceback (most recent call last):
File "C:\Users\rahul\AppData\Local\Programs\Python\Python37-32\lib\logging\config.py", line 753, in add_handlers
logger.addHandler(self.config['handlers'][h])
File "C:\Users\rahul\AppData\Local\Programs\Python\Python37-32\lib\logging\config.py", line 315, in __getitem__
value = dict.__getitem__(self, key)
KeyError: 'file_handler'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\rahul\AppData\Local\Programs\Python\Python37-32\lib\logging\config.py", line 634, in configure
self.configure_root(root)
File "C:\Users\rahul\AppData\Local\Programs\Python\Python37-32\lib\logging\config.py", line 786, in configure_root
self.common_logger_config(root, config, incremental)
File "C:\Users\rahul\AppData\Local\Programs\Python\Python37-32\lib\logging\config.py", line 770, in common_logger_config
self.add_handlers(logger, handlers)
File "C:\Users\rahul\AppData\Local\Programs\Python\Python37-32\lib\logging\config.py", line 755, in add_handlers
raise ValueError('Unable to add handler %r' % h) from e
ValueError: Unable to add handler 'file_handler'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "F:\Codedata\scratch\Python\ManufactureDataWithDateRange\ActionHandler.py", line 17, in <module>
logging.config.dictConfig(json.load(fp))
File "C:\Users\rahul\AppData\Local\Programs\Python\Python37-32\lib\logging\config.py", line 792, in dictConfig
dictConfigClass(config).configure()
File "C:\Users\rahul\AppData\Local\Programs\Python\Python37-32\lib\logging\config.py", line 637, in configure
'logger') from e
ValueError: Unable to configure root logger
注意:,当我尝试在以json格式打印日志之前尝试在json文件中添加“ streamHandler”时,就会发生这种情况。我认为“ config”中存在问题.dictConfig” ,我不知道...请帮助我。 没有“ StreamHandler”的工作程序是:
{
"version": 1,
"disable_existing_loggers": false,
"formatters": {
"json": {
"format": "%(asctime)s - %(name)s - %(levelname)s - %(message)s",
"()": "pythonjsonlogger.jsonlogger.JsonFormatter"
}
},
"handlers": {
"file_handler": {
"class": "logging.handlers.RotatingFileHandler",
"level": "DEBUG",
"formatter": "json",
"filename": "Data-Generation.log",
"encoding": "utf8"
}
},
"root": {
"level": "DEBUG",
"handlers": ["file_handler"]
}
}
这是正确的Json格式并打印日志,但想从我的日志中删除所有不需要的行,因此我尝试包括“ streamHnadler”,请帮助我该怎么做...谢谢 < / p>