使用正则表达式解析log4j日志文件[英] parsing log4j log file using regular expression

本文是小编为大家收集整理的关于使用正则表达式解析log4j日志文件的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我创建了一个Java应用程序,用于使用正则表达式解析Log4j日志文件,该应用程序适用于我在下面显示的日志

1999-11-27 15:49:37,459 [thread-x] ERROR mypackage - Catastrophic system failure

但不适用于

2015-01-22 01:52:54,237 [http-bio-80-exec-5] FATAL   TestLog4jServlet - Show FATAL message

我的log4j conversionpattern在下面给出

log4j.appender.Appender2.layout.ConversionPattern=%d [%t] %-7p %10c{1} - %m%n

任何人都可以告诉我一些解决方案

我的代码如下

给出
public static void main(String[] args) {
    String regex = "(\\d{4}-\\d{2}-\\d{2}) (\\d{2}:\\d{2}:\\d{2},\\d{3}) \\[(.*)\\] ([^ ]*) ([^ ]*) - (.*)$";

    Pattern p = Pattern.compile(regex);
    String[] samples = {
            "2015-01-22 01:52:54,237 [http-bio-80-exec-5] FATAL   TestLog4jServlet - Show FATAL message"
        };

    Matcher m = p.matcher(samples[1]);
    System.out.println(m.matches());
    if (m.matches() && m.groupCount() == 6) {
        String date = m.group(1);
        String time = m.group(2);
        String threadId = m.group(3);
        String priority = m.group(4);
        String category = m.group(5);
        String message = m.group(6);

        System.out.println("date: " + date);
        System.out.println("time: " + time);
        System.out.println("threadId: " + threadId);
        System.out.println("priority: " + priority);
        System.out.println("category: " + category);
        System.out.println("message: " + message);
    }
}

推荐答案

因为FATAL和TestLog4jServlet之间有两个空间,但是您的正则是一个空间.因此,我建议您用<space>+替换相应的空间,该空间允许一个或多个空间.

(\d{4}-\d{2}-\d{2}) (\d{2}:\d{2}:\d{2},\d{3}) \[(.*?)\] ([^ ]*) +([^ ]*) - (.*)$
                                                                ^
                                                                |

demo

java正则是

"(\\d{4}-\\d{2}-\\d{2}) (\\d{2}:\\d{2}:\\d{2},\\d{3}) \\[(.*)\\] ([^ ]*) +([^ ]*) - (.*)$"

其他推荐答案

我认为LogStash更适合解析日志.

本文地址:https://www.itbaoku.cn/post/1574964.html

问题描述

I have created a java application for parsing the log4j log file using regular expression, The application is working fine for the log which i have shown below

1999-11-27 15:49:37,459 [thread-x] ERROR mypackage - Catastrophic system failure

but not working for

2015-01-22 01:52:54,237 [http-bio-80-exec-5] FATAL   TestLog4jServlet - Show FATAL message

My log4j ConversionPattern is given below

log4j.appender.Appender2.layout.ConversionPattern=%d [%t] %-7p %10c{1} - %m%n

Can anyone please tell me some solution for this

My code is as given below

public static void main(String[] args) {
    String regex = "(\\d{4}-\\d{2}-\\d{2}) (\\d{2}:\\d{2}:\\d{2},\\d{3}) \\[(.*)\\] ([^ ]*) ([^ ]*) - (.*)$";

    Pattern p = Pattern.compile(regex);
    String[] samples = {
            "2015-01-22 01:52:54,237 [http-bio-80-exec-5] FATAL   TestLog4jServlet - Show FATAL message"
        };

    Matcher m = p.matcher(samples[1]);
    System.out.println(m.matches());
    if (m.matches() && m.groupCount() == 6) {
        String date = m.group(1);
        String time = m.group(2);
        String threadId = m.group(3);
        String priority = m.group(4);
        String category = m.group(5);
        String message = m.group(6);

        System.out.println("date: " + date);
        System.out.println("time: " + time);
        System.out.println("threadId: " + threadId);
        System.out.println("priority: " + priority);
        System.out.println("category: " + category);
        System.out.println("message: " + message);
    }
}

推荐答案

Because there are two spaces between FATAL and TestLog4jServlet but you included only one space in your regex. So i suggest you to replace the corresponding space with <space>+ which allows one or more spaces.

(\d{4}-\d{2}-\d{2}) (\d{2}:\d{2}:\d{2},\d{3}) \[(.*?)\] ([^ ]*) +([^ ]*) - (.*)$
                                                                ^
                                                                |

DEMO

Java regex would be,

"(\\d{4}-\\d{2}-\\d{2}) (\\d{2}:\\d{2}:\\d{2},\\d{3}) \\[(.*)\\] ([^ ]*) +([^ ]*) - (.*)$"

其他推荐答案

I think the Logstash is better for parsing log.