Java 日志的正则表达式模式

Regex Pattern for a Java Log

我正在尝试使用 fluentd 中的正则表达式解析器插件来索引我的应用程序的日志。

这是它的一个片段。

2020-05-06T22:34:50.860-0700 - WARN [main] o.s.b.GenericTypeAwarePropertyDescriptor: Invalid JavaBean property 'pipeline' being accessed! Ambiguous write methods found next to actually used [public void com.theoaal.module.pipeline.mbean.DynamicPhaseExecutionConfigurationMBeanBuilder.setPipeline(com.theplatform.module.pipeline.DynamicPipeline)]: [public void com.theplatform.module.pipeline.mbean.PhaseExecutionConfigurationMBeanBuilder.setPipeline(com.theoaal.module.pipeline.Pipeline)]

我已经使用 regex101.com 来匹配正则表达式模式,但无法匹配。

^(?<date>\d{4}\-\d{2}\-\d{2})(?<timestamp>[A-Z][a-z]{1}\d{2}:\d{2}:\d{2}.\d{3}\-\d{4})\s\-\s(?<loglevel>\[\w\]{6})\s+(?<class>\[[A-Z][a-z]+\])\s(?<message>.*)$

请帮忙。 谢谢

您可以使用

^(?<date>\d{4}-\d{2}-\d{2})[A-Z](?<timestamp>\d{2}:\d{2}:\d{2}\.\d{3}-\d{4})\s+-\s+(?<loglevel>\w+)\s+(?<class>\[\w+\])\s+(?<message>.*)

regex demo

请注意,在您的模式中,\[\w\]{6} 仅匹配 [,一个单词字符和六个 ] 个字符。在时间戳模式中,[A-Z][a-z]{1} 需要两个字母,但只有一个 T。您的 "class" 模式需要一个带有 [A-Z][a-z]+ 的大写单词,但 main 全部小写。您不必要地在字符 类 之外转义 -,并且未能转义模式中的文字点。

详情

  • ^ - 字符串开头
  • (?<date>\d{4}-\d{2}-\d{2}) - 日期:4 位数字,-,2 位数字,-,2 位数字
  • [A-Z] - 大写 ASCII 字母[=7​​1=]
  • (?<timestamp>\d{2}:\d{2}:\d{2}\.\d{3}-\d{4}) - 2 位数字,:,2 位数字,:,2 位数字,.,3 位数字,- 和 4 位数字
  • \s+-\s+ - - 包含 1+ 个空格
  • (?<loglevel>\w+) - 1+ 个单词字符
  • \s+ - 1+ 个空格
  • (?<class>\[\w+\]) - [, 1+ 个字符, ]
  • \s+ - 1+ 个空格
  • (?<message>.*) - 行的资源。

复制并粘贴到 fluent.conftd-agent.conf:

<source>
  type tail
  path /var/log/foo/bar.log
  pos_file /var/log/td-agent/foo-bar.log.pos
  tag foo.bar
  format /^(?<date>\d{4}-\d{2}-\d{2})[A-Z](?<timestamp>\d{2}:\d{2}:\d{2}\.\d{3}-\d{4})\s+-\s+(?<loglevel>\w+)\s+(?<class>\[\w+\])\s+(?<message>.*)/
</source>

Test: