如何检查日志条目是否使用给定的ConversionPattern编写? [英] How to check if a log entry was written with a given ConversionPattern?

查看:65
本文介绍了如何检查日志条目是否使用给定的ConversionPattern编写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多日志文件,并且我有几个log4j配置(文件追加程序的不同ConversionPattern格式).

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

我想编写一个脚本/测试,该脚本/测试能够通过用于写入文件的ConversionPattern对日志文件进行分组.我缺少的一点是如何实现类似的东西:

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)

Log4j API是否可以?

Is it possible with Log4j API?

上述方法对于给定的值应返回true:

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

推荐答案

GROK非常适合您的用例.我最近一直在使用logstash,但是可以在github上尝试使用java-grok项目.我发现此链接来自 另一个stackoverflow答案.

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本质上是一个正则表达式引擎.它有一堆预定义的正则表达式,可以匹配最常见的日志记录条目,例如时间戳,日志记录级别等.如果这样对您不起作用,您是否考虑过使用直接的正则表达式?

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?

这篇关于如何检查日志条目是否使用给定的ConversionPattern编写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆