[logger_main]
level=INFO
handlers=file_handler
qualname=main
propagate=0
[handler_file_handler]
class=FileHandler
formatter=formatter
class=handlers.RotatingFileHandler
maxBytes=31457280
level=DEBUG
args=('../log/logfile.txt',)
[formatter_formatter]
format=%(asctime)s - %(RequestNumber)s - %(funcName)-20s() - %(levelname)-10s - %(message)s
format=%(asctime)s - %(RequestNumber)s - %(funcName)-20s() - %(levelname)-10s - %(message)s
在Python中,我使用记录器适配器更新写入文件的每个日志中的请求编号:
log = logging.LoggerAdapter(logging.getLogger('main'), {"RequestNumber" : requestNum()});
抛出错误:下面是错误的追溯 -
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: 'RequestNumber'
答案 0 :(得分:0)
适合我。
>>> import logging
>>> logger = logging.getLogger('foo')
>>> formatter = logging.Formatter('%(customid)s %(message)s')
>>> handler = logging.StreamHandler()
>>> handler.setFormatter(formatter)
>>> logger.addHandler(handler)
>>> adapter = logging.LoggerAdapter(logger, {'customid': 'foo'})
>>> adapter.warning('test')
foo test