log4j:防止重复日志消息的标准方法?
我们的生产应用程序在无法建立 TCP/IP 连接时会记录错误。由于它不断地重试连接,因此它会一遍又一遍地记录相同的错误消息。同样,如果某些实时资源在一段时间内不可用,应用程序中的其他正在运行的组件可能会进入错误循环。
是否有任何标准方法来控制记录相同错误的次数? (我们使用的是 log4j,所以如果 log4j 有任何扩展来处理这个问题,那就完美了。)
Our production application logs an error when it fails to establish a TCP/IP connection. Since it is constantly retrying the connection, it logs the same error message over and over. And similarly, other running components in the application can get into an error loop if some realtime resource is unavailable for a period of time.
Is there any standard approach to controlling the number of times the same error gets logged? (We are using log4j, so if there is any extension for log4j to handle this, it would be perfect.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我刚刚创建了一个 Java 类,它使用 log4j 解决了这个问题。当我想记录一条消息时,我只需执行以下操作:
而不是:
这使得它每 5 秒最多记录 1 次,并打印它应该记录的次数(例如 |x53|)。显然,您可以这样做,这样您就没有那么多参数,或者通过执行 log.warn 或其他操作来拉出级别,但这适用于我的用例。
I just created a Java class that solves this exact problem using log4j. When I want to log a message, I just do something like this:
Instead of:
Which makes it log a maximum of 1 time ever 5 seconds, and prints how many times it should have logged (e.g. |x53|). Obviously, you can make it so you don't have as many parameters, or pull the level out by doing log.warn or something, but this works for my use case.
通过在每次记录错误时记录时间戳,然后仅在经过特定时间段后才在下次记录它,来控制这一点相当简单。
理想情况下,这将是 log4j 中的一个功能,但在应用程序中对其进行编码也不算太糟糕,并且您可以将其封装在辅助类中,以避免在整个代码中出现样板文件。
显然,每个重复的日志语句都需要某种唯一的 ID,以便您可以合并来自同一源的语句。
It would be fairly simple to control this by recording a timestamp each time you log the error, and then only logging it next time if a certain period has elapsed.
Ideally this would be a feature within log4j, but coding it within your app isn't too bad, and you could encapsulate it within a helper class to avoid boilerplate throughout your code.
Clearly, each repetitive log statement would need some kind of unique ID so that you could merge statements from the same source.