【问题标题】:Regex Pattern for a Java LogJava 日志的正则表达式模式
【发布时间】:2020-05-08 10:09:50
【问题描述】:

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

这是它的一个sn-p。

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>.*)$

请帮助。 谢谢

【问题讨论】:

    标签: regex logging fluentd


    【解决方案1】:

    你可以使用

    ^(?<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} 需要两个字母,但 tere 是单个 T。您的“类”模式需要[A-Z][a-z]+ 的大写单词,但main 全部小写。您在字符类之外不必要地转义 -,并且您未能转义模式中的文字点。

    详情

    • ^ - 字符串开头
    • (?&lt;date&gt;\d{4}-\d{2}-\d{2}) - 日期:4 位,-,2 位,-,2 位
    • [A-Z] - 一个大写的 ASCII 字母
    • (?&lt;timestamp&gt;\d{2}:\d{2}:\d{2}\.\d{3}-\d{4}) - 2 位,:,2 位,:,2 位,.,3 位,- 和 4 位
    • \s+-\s+ - - 包含 1+ 个空格
    • (?&lt;loglevel&gt;\w+) - 1+ 字字符
    • \s+ - 1+ 个空格
    • (?&lt;class&gt;\[\w+\]) - [,1+ 字字符,]
    • \s+ - 1+ 个空格
    • (?&lt;message&gt;.*) - 行的 res。

    复制粘贴到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:

    【讨论】:

    • 我在 parrs.conf 文件中添加了相同的内容,但没有得到预期的输出。当我试图在 Splunk 仪表板上获取它时,整条线被视为一个事件
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-09
    • 1970-01-01
    相关资源
    最近更新 更多