如何仅记录一次重复警告
有一种模式时常发生。我有一个方法被调用了很多次,它包含这个片段:
Foo foo = getConfiguredFoo();
if (foo == null) {
logger.warn("Foo not configured");
foo = getDefaultFoo();
}
然后我的日志文件被这个警告弄乱了一百次。我知道我可以grep
它,但我想知道是否有更好的方法来只看到这个警告一次。
注意:默认情况下,消息重复是正确的行为,因此这与 避免无意的重复日志消息。我将我的问题标记为 log4j,但我对其他 java 日志框架持开放态度。
There is a pattern that happens every now and then. I have a method called many times, and it contains this snippet:
Foo foo = getConfiguredFoo();
if (foo == null) {
logger.warn("Foo not configured");
foo = getDefaultFoo();
}
Then my log file is cluttered with this warning a hundred times. I know I can grep
it out, but I wonder if there is a better way to see this warning only once.
Note: the duplication of messages is a correct behavior by default, so this is not about avoiding unintentional duplicate log message. I tagged my question as log4j, but I'm open to other java logging frameworks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是我能想到的:一个累积警告的类,可以在最后转储这些警告。这是很常规的,但你可以明白要点。当然,可以自定义转储部分以使用记录器。
Here is what I can come up with: a class that accumulates warnings which can be dumped at the end. It's in groovy, but you can get the point. The dumping part can be customized to use a logger, of course.
我不久前遇到过类似的问题,但在 Log4J 中找不到任何处理此问题的方法。
我最终做了以下操作:
如果您有一两个日志语句,您不想在日志中看到重复,但又不想用更多日志语句进行扩展(您需要为每条记录的消息提供一个布尔值),则此解决方案是可以的
I faced a similar problem sometime ago but could not find any way of dealing with this in Log4J.
I finally did the following:
This solution is OK if you have one or two log statements you don't want to see repeated in your logs but does not scale up with more log statements (you need a boolean for every message logged)
您可以在日志记录周围编写一个包装器来存储记录的最后一行。根据您的实现方式,您可以添加某种计数器来记录记录的次数,或者您可以选择子类化 Logger 而不是使用外部包装器。如果您也需要的话,可以使用布尔抑制重复进行配置。
You could write a wrapper around your logging to store the last line logged. Depending on how you implement, you could add some sort of counter to log how many times it got logged or you may choose to subclass Logger instead of having an external wrapper. Could be configurable with a boolean suppressDuplicates if you needed that too.
如果这是您想要打印一次的唯一内容,那么使用保存的布尔值将是您最好的选择。如果您想要可以在整个项目中使用的东西,我已经创建了一些可能有用的东西。我刚刚创建了一个使用 log4j 记录器实例的 Java 类。当我想记录一条消息时,我只需执行以下操作:
而不是:
这使得它每 5 秒最多记录 1 次,并打印它应该记录的次数(例如 |x53|)。显然,您可以这样做,这样您就没有那么多参数,或者通过执行 log.warn 或其他操作来拉出级别,但这适用于我的用例。
对于您来说(如果您只想每次打印一次),这有点过分了,但您仍然可以通过传入类似以下内容来实现:Long.MAX_LONG 作为第三个参数。我喜欢能够确定每个特定日志消息的频率(因此是参数)的灵活性。例如,这将实现您想要的:
这是 LogConsolidated 类:
If this is the only thing you want to print one time, then using a saved boolean would be your best bet. If you wanted something you could use throughout your project, I have created something that may be useful. I just created a Java class that uses a log4j logger instance. When I want to log a message, I just do something like this:
Instead of:
Which makes it log a maximum of 1 time every 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.
For you (if you only want to print one time, every time) this is overkill, but you can still do it by passing in something like: Long.MAX_LONG in as the 3rd parameter. I like the flexibility to be able to determine frequency for each specific log message (hence the parameter). For example, this would accomplish what you want:
Here is the LogConsolidated class: