Python日志记录:捆绑重复出现的消息 [英] Python logging: bundle reoccurring messages

查看:45
本文介绍了Python日志记录:捆绑重复出现的消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将相等的连续消息与python的日志记录模块捆绑在一起.这样,我想防止其中包含成千上万条相等消息的巨大日志文件.取而代之的是,日志仅应针对第1,第2,第3,第10,第50,第100,第500等时间,直到将不同的消息发送到记录器.

I would like to bundle equal consecutive messages with python's logging module. This way I want to prevent huge log files with thousands of equal messages in them. Instead, the log should only be written for the, e.g. 1st, 2nd, 3rd, 10th, 50th, 100th, 500th, ... time until a different messages is sent to the logger.

如何使用记录模块来实现?

How can this be achieved with the logging module?

这里是一个例子:
代替这个:

Here is an example:
Instead of this:

2017-07-16 16:00:00 INFO: File upload initiated
2017-07-16 16:01:00 ERROR: I/O Error: no route to host
2017-07-16 16:02:00 ERROR: I/O Error: no route to host
2017-07-16 16:03:00 ERROR: I/O Error: no route to host
2017-07-16 16:04:00 ERROR: I/O Error: no route to host
2017-07-16 16:05:00 ERROR: I/O Error: no route to host
... [169 more lines]
2017-07-16 18:54:00 ERROR: I/O Error: no route to host
2017-07-16 18:55:00 ERROR: I/O Error: no route to host
2017-07-16 18:56:00 INFO: File upload started

我希望它们以任何方式捆绑在一起,例如:

I want them to be bundle in any way, e.g.:

2017-07-16 16:00:00 INFO: File upload initiated
2017-07-16 16:01:00 ERROR: I/O Error: no route to host
2017-07-16 16:02:00 ERROR: I/O Error: no route to host
2017-07-16 16:03:00 ERROR: I/O Error: no route to host
2017-07-16 16:13:00 ERROR (repeated 10x): I/O Error: no route to host
2017-07-16 17:03:00 ERROR (repeated 50x): I/O Error: no route to host
2017-07-16 18:43:00 ERROR (repeated 100x): I/O Error: no route to host
2017-07-16 18:55:00 ERROR (repeated 12x): I/O Error: no route to host
2017-07-16 18:56:00 INFO: File upload started

推荐答案

所以我最终扩展了Logger类并自己添加了所需的功能.

So I ended up extending the Logger class and adding the desired functionality myself.

访问 github存储库以获取代码.

这篇关于Python日志记录:捆绑重复出现的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆