Is there an easy way to find out programmatically the total number of messages that have been logged at a certain level since the program started? I could do that by writing a wrapper around a Logger and using it everywhere, but perhaps there is an easier way?
Asked
Active
Viewed 765 times
3
-
`grep WARN mylog.log | wc -l`? Or do you intend for your program to observe the logs and change the behaviour based on it? – Kayaman Aug 31 '17 at 07:37
-
1slf4j cannot do this because it is the api. You must define this in your backend. For logback it is quite simple - just use another appender which you write, which subclass your current one. It is then easy to add the statistics collecting. – Thorbjørn Ravn Andersen Aug 31 '17 at 08:22
-
1Assuming you're using log4j2 [this answer](https://stackoverflow.com/a/45039509/1654233) may help so you end up with either a custom `CountingRewritePolicy` combined with `RewriteAppender` or a custom `LogEventFactory`. – yegodm Aug 31 '17 at 08:50
-
@yegodm I am using log4j2. Thanks, that looks promising, I'll try it. – biggvsdiccvs Aug 31 '17 at 09:43
-
2@Kayaman I know how to analyze the logs. The purpose of this is to add warning and error counts to the report produced by the program as an additional sanity check. Nobody usually reads the debug logs unless there is an obvious problem, but people do look at the report. – biggvsdiccvs Aug 31 '17 at 09:52
-
Nobody reads debug logs, but you should read warning logs. That's kind of the idea of the different levels of logging. – Kayaman Aug 31 '17 at 10:33
-
That's one possible use of the different levels of logging. Thanks for the sage advice. – biggvsdiccvs Aug 31 '17 at 18:22