Python logging.conf 重新加载配置
Python logging.conf reload configuration
在我的 python 代码中,我从一个文件中读取了日志记录配置,我在该文件中放置了我希望在我的代码中使用的格式。
当我尝试在运行时重新加载配置时,我放入格式化程序的一些额外环境似乎丢失了,并且出现以下错误。
Traceback (most recent call last):
File "/usr/lib64/python2.7/logging/__init__.py", line 861, in emit
msg = self.format(record)
File "/usr/lib64/python2.7/logging/__init__.py", line 734, in format
return fmt.format(record)
File "/usr/lib64/python2.7/logging/__init__.py", line 469, in format
s = self._fmt % record.__dict__
KeyError: 'hostname'
Logged from file conn.py, line 304
注意我的logging.conf如下:
[formatter_simpleFormatter]
format=[%(levelname)s] %(asctime)s [ThId-%(threadName)-10s] [%(filename)s:%(funcName)s:%(lineno)s] [%(hostname)s] %(message)s
datefmt=%m/%d/%Y %I:%M:%S %p
其中 hostname 在代码中通过以下方式设置:
class HostnameFilter(logging.Filter):
hostname = platform.node()
def filter(self, record):
record.hostname = HostnameFilter.hostname
return True
## Log Logger.
class Logger:
"""!@brief
Logger wrapper
"""
def __init__(self):
self.log = logging.getLogger("MYCONF")
self.log.addFilter(HostnameFilter())
奇怪的是,错误似乎出现在 类 (conn.py) 中,而不是我的,也许这是我正在使用的一些库的代码,它们也在记录日志。
这是流量吗?要我做点别的吗?
因为格式化发生在处理程序中,并且记录到记录器的事件会传递给该记录器的处理程序以及记录器层次结构中更高层的处理程序,因此您需要确保 hostname
属性始终添加到记录中 - 如果使用不同的记录器记录消息,情况可能并非如此。尝试将过滤器附加到相关的处理程序而不是记录器。
在我的 python 代码中,我从一个文件中读取了日志记录配置,我在该文件中放置了我希望在我的代码中使用的格式。
当我尝试在运行时重新加载配置时,我放入格式化程序的一些额外环境似乎丢失了,并且出现以下错误。
Traceback (most recent call last):
File "/usr/lib64/python2.7/logging/__init__.py", line 861, in emit
msg = self.format(record)
File "/usr/lib64/python2.7/logging/__init__.py", line 734, in format
return fmt.format(record)
File "/usr/lib64/python2.7/logging/__init__.py", line 469, in format
s = self._fmt % record.__dict__
KeyError: 'hostname'
Logged from file conn.py, line 304
注意我的logging.conf如下:
[formatter_simpleFormatter]
format=[%(levelname)s] %(asctime)s [ThId-%(threadName)-10s] [%(filename)s:%(funcName)s:%(lineno)s] [%(hostname)s] %(message)s
datefmt=%m/%d/%Y %I:%M:%S %p
其中 hostname 在代码中通过以下方式设置:
class HostnameFilter(logging.Filter):
hostname = platform.node()
def filter(self, record):
record.hostname = HostnameFilter.hostname
return True
## Log Logger.
class Logger:
"""!@brief
Logger wrapper
"""
def __init__(self):
self.log = logging.getLogger("MYCONF")
self.log.addFilter(HostnameFilter())
奇怪的是,错误似乎出现在 类 (conn.py) 中,而不是我的,也许这是我正在使用的一些库的代码,它们也在记录日志。
这是流量吗?要我做点别的吗?
因为格式化发生在处理程序中,并且记录到记录器的事件会传递给该记录器的处理程序以及记录器层次结构中更高层的处理程序,因此您需要确保 hostname
属性始终添加到记录中 - 如果使用不同的记录器记录消息,情况可能并非如此。尝试将过滤器附加到相关的处理程序而不是记录器。