Java正则表达式解析复杂跨行日志

Java正则表达式解析复杂跨行日志


使用正则表达式解析日志

解析内容

String content= "2023-09-23 11:31:54.705  INFO [           main] com.zlm.tools.ToolsApplication           : Starting ToolsApplication using Java 1.8.0_201 on \n" +
                "thinkbook16 with PID 2904 (D:\\study\\tools\\target\\classes started by zlm in D:\\study\\tools)\n" +
                "2023-09-23 11:31:54.706  INFO [           main] com.zlm.tools.ToolsApplication           : No active profile set, falling back to 1 default profile: \"default\"\n" +
                "2023-09-23 11:31:55.190  INFO [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)\n" +
                "2023-09-23 11:31:55.194  INFO [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]\n" +
                "2023-09-23 11:31:55.195  INFO [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.78]\n" +
                "2023-09-23 11:31:55.261  INFO [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext\n" +
                "2023-09-23 11:31:55.261  INFO [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 532 ms\n" +
                "2023-09-23 11:31:55.448  INFO [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''\n" +
                "2023-09-23 11:31:55.453  INFO [           main] com.zlm.tools.ToolsApplication           : Started ToolsApplication in 0.943 seconds (JVM running for 1.73)";

正则

//匹配 []内容包含[]
String regex = "\\[(.+)\\]"
//匹配任意字符非贪婪模式
regex = "(.+?)"
//匹配:开始 非[开始的内容 
regex = ":([^\\[]*\\n)+"
//匹配日期
regex = "(\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}\\.\\d{3})";
//匹配日志级别
regex = "([INFO, WARN, ERROR, FATAL, TRACE, DEBUG, INFO]+)";
String regex = "(\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}\\.\\d{3})  ([INFO, WARN, ERROR, FATAL, TRACE, DEBUG, INFO]+) \\[(.+)\\] (.+?) :([^\\[]*\\n)+";
        

使用

Pattern compile = Pattern.compile(regex);
        Matcher matcher = compile.matcher(content);
        while (matcher.find()){
            System.out.println("执行时间: "+matcher.group(1));
            System.out.println("日志级别: "+matcher.group(2));
            System.out.println("执行方法: "+matcher.group(3).trim());
            System.out.println("执行类: "+matcher.group(4));
            System.out.println("日志内容: "+matcher.group(5));
        }

完整代码

public void test(){
        String content= "2023-09-23 11:31:54.705  INFO [           main] com.zlm.tools.ToolsApplication           : Starting ToolsApplication using Java 1.8.0_201 on \n" +
                "thinkbook16 with PID 2904 (D:\\study\\tools\\target\\classes started by zlm in D:\\study\\tools)\n" +
                "2023-09-23 11:31:54.706  INFO [           main] com.zlm.tools.ToolsApplication           : No active profile set, falling back to 1 default profile: \"default\"\n" +
                "2023-09-23 11:31:55.190  INFO [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)\n" +
                "2023-09-23 11:31:55.194  INFO [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]\n" +
                "2023-09-23 11:31:55.195  INFO [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.78]\n" +
                "2023-09-23 11:31:55.261  INFO [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext\n" +
                "2023-09-23 11:31:55.261  INFO [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 532 ms\n" +
                "2023-09-23 11:31:55.448  INFO [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''\n" +
                "2023-09-23 11:31:55.453  INFO [           main] com.zlm.tools.ToolsApplication           : Started ToolsApplication in 0.943 seconds (JVM running for 1.73)";
        String regex = "(\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}\\.\\d{3})  ([INFO, WARN, ERROR, FATAL, TRACE, DEBUG, INFO]+) \\[(.+)\\] (.+?) :([^\\[]*\\n)+";
        Pattern compile = Pattern.compile(regex);
        Matcher matcher = compile.matcher(content);
        while (matcher.find()){
            System.out.println("执行时间: "+matcher.group(1));
            System.out.println("日志级别: "+matcher.group(2));
            System.out.println("执行方法: "+matcher.group(3).trim());
            System.out.println("执行类: "+matcher.group(4));
            System.out.println("日志内容: "+matcher.group(5));
        }

    }

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值