How to check if a log entry was written with a giv

2019-08-10 09:08发布

问题:

I have many log files and I have a few log4j configurations (different ConversionPattern formats for file appenders).

I would like to write a script/test which is able to group log files by a ConversionPattern which was used to write a file. The bit I'm missing is how to implement something like:

boolean logToPatternMatcher.matches(String conversionPattern, String logFileEntry)

Is it possible with Log4j API?

The above method should return true for a given:

String conversionPattern = "%d{ISO8601} %-5p [%-16.16t][%c] %m%n"

String logFileEntry = "2015-02-12 00:02:38,023 WARN [pool-58-thread-1][some.package.name.ConvertingPublisher] Document [type: app.MessageProcessed, id: 1063_1_20150128072222800] DUPLICATED."

回答1:

GROK is perfect for your use case. I have been using it with logstash recently but there is a java-grok project on github you could try. I found this link from another stackoverflow answer.

GROK is essentially a regexp engine. It has a bunch of pre-defined regexp's that will match most common logging entries like timestamps, logging level etc. If that doesn't work out for you have you thought about using straight regexp?



标签: java log4j