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

标签 python logging error-logging

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

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/

相关文章:

bash - 运行 kubectl exec 时禁用 Kubernetes 上的网络日志

c++ - 记录函数的返回值

php - Docker中的Apache/PHP error_log位置?

oracle - 如何在 Oracle 中开发一个 after serverror 触发器?

python - 请求响应中的非 'ascii' 字符

python - Keras 模型为多标签图像分类提供非常低的训练和验证精度

python - 有什么办法可以把它变成列表理解

Python numpy, reshape /转换数组避免遍历行

python - 从 python 脚本将日志写入/var/log 的最佳实践?

c# - NLog 异步目标数据库连接异常 - 如何获取它们?