gpt4 book ai didi

python - 在 Python 日志记录模块 AKA 日志压缩中抑制具有相同内容的多条消息

转载 作者:太空狗 更新时间:2023-10-29 17:18:07 24 4
gpt4 key购买 nike

根据设计,我的应用程序有时会产生重复的错误,这些错误会填满日志文件并让人难以阅读。看起来像这样:

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 testWARNING:root:my repeated testWARNING:root:my repeated testWARNING:root:my repeated testWARNING:root:my other testWARNING:root:my testWARNING:root:my repeated testWARNING:root:my other test

关于python - 在 Python 日志记录模块 AKA 日志压缩中抑制具有相同内容的多条消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44691558/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com