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

2019-08-10 08:55发布

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."

标签: java log4j
1条回答
混吃等死
2楼-- · 2019-08-10 09:27

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?

查看更多
登录 后发表回答