根据设计,我的应用程序有时会产生重复的错误,这些错误会填满日志文件并让人难以阅读。看起来像这样:
WARNING:__main__:CRON10: clock unset or no wind update received in 60 sec -> supressed rrd update
WARNING:__main__:CRON10: clock unset or no wind update received in 60 sec -> supressed rrd update
WARNING:__main__:CRON10: clock unset or no wind update received in 60 sec -> supressed rrd update
WARNING:__main__:CRON10: clock unset or no wind update received in 60 sec -> supressed rrd update
如何使用 Python 日志记录模块来抑制重复消息并输出更多 rsyslog 样式的内容 (http://www.rsyslog.com/doc/rsconf1_repeatedmsgreduction.html):
WARNING:__main__:CRON10: clock unset or no wind update received in 60 sec -> supressed rrd update
--- The last message repeated 3 times
有没有办法扩展日志记录,或者我是否必须编写一个完全自己的记录器?
我用于记录的代码是:
logging.basicConfig(format='%(asctime)s %(message)s')
logging.basicConfig(level=logging.info)
logger = logging.getLogger(__name__)
hdlr = logging.FileHandler(LOGFILE)
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
有什么想法吗?
最佳答案
您可以创建一个 logging.Filter
这将跟踪最后记录的记录并过滤掉任何重复(相似)的记录,例如:
import logging
class DuplicateFilter(logging.Filter):
def filter(self, record):
# add other fields if you need more granular comparison, depends on your app
current_log = (record.module, record.levelno, record.msg)
if current_log != getattr(self, "last_log", None):
self.last_log = current_log
return True
return False
然后只需将它添加到您使用的记录器/处理程序(即 hdlr.addFilter(DuplicateFilter())
)或根记录器以过滤所有默认日志。这是一个简单的测试:
import logging
logging.warn("my test")
logging.warn("my repeated test")
logging.warn("my repeated test")
logging.warn("my repeated test")
logging.warn("my other test")
logger = logging.getLogger() # get the root logger
logger.addFilter(DuplicateFilter()) # add the filter to it
logging.warn("my test")
logging.warn("my repeated test")
logging.warn("my repeated test")
logging.warn("my repeated test")
logging.warn("my other test")
这将打印出:
WARNING:root:my test WARNING:root:my repeated test WARNING:root:my repeated test WARNING:root:my repeated test WARNING:root:my other test WARNING:root:my test WARNING:root:my repeated test WARNING:root:my other test
关于python - 在 Python 日志记录模块 AKA 日志压缩中抑制具有相同内容的多条消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44691558/