3

我们正在使用 LogMX 日志查看器来监控我们的应用程序日志,使用正则表达式解析器。

每次日志消息包含“-”字符时,LogMX 都不会按预期解析日志事件。

例如,以下日志事件:

[ERROR] | com.nsoft.gmonitor.Controller - File Loader - Error while loading file "C:\GMonitor\prefs.properties - Copy"

被解析为:

  • 发射器: com.nsoft.gmonitor.Controller - File Loader

  • 线: Error while loading file "C:\GMonitor\prefs.properties

  • 信息:- Copy"

代替:

  • 发射器:com.nsoft.gmonitor.Controller

  • 线:File Loader

  • 信息:Error while loading file "C:\GMonitor\prefs.properties- Copy"

我们正在使用以下正则表达式:

\[(.*)\] \| (.*) - (.*) - (.*)

谢谢您的帮助。

4

2 回答 2

4

You should use this regexp instead:

\[(.*)\] \| (.*?) - (.*?) - (.*)

I just added a ? character after .* for 'Emitter' and 'Thread' fields/groups. This is a common regular-expression issue (not specific to LogMX):

  • .* is called a greedy quantifier: it means that it will try to match the maximum number of characters

  • .*? is called a reluctant quantifier: it means that it will try to match the minimum number of characters

You can read more about this in JDK API (search for 'greedy') or LogMX docs.

PS : if you don't want to use regular expressions to parse your logs in LogMX, you could use its "Log4j/Logback pattern Parsers" instead: the pattern [%p] | %c - %t - %m will match your need, and is reluctant by default for all fields/groups by default.

于 2014-06-25T21:07:54.590 回答
2

那是因为你的正则表达式是贪婪的。尝试添加?到您的组以避免正则表达式贪婪。

看看这个:

\[.*\] .? (.*?) - (.*?) - (.*)

正则表达式可视化

调试演示

在这里,您可以看到存储在组中的正确值:

在此处输入图像描述

于 2014-06-23T22:18:49.563 回答