• 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)";
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    正则

    //匹配 []内容包含[]
    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]+)";
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    String regex = "(\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}\\.\\d{3})  ([INFO, WARN, ERROR, FATAL, TRACE, DEBUG, INFO]+) \\[(.+)\\] (.+?) :([^\\[]*\\n)+";
            
    
    • 1
    • 2

    使用

    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));
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    完整代码

    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));
            }
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    在这里插入图片描述

  • 相关阅读:
    四轴飞控DIY Mark4 - 整理&参数优化
    记录开发过程中遇到的oracle 分页问题
    Java枚举
    达观RPA实战-自定义控件创建excel表头
    2023全新云渲染测评!效果图渲染哪个平台性价比更高?
    关于海洋湍流的一些笔记
    Windows批处理
    vue3中的watch可能是比watchEffect更好的选择
    基于SpringBoot+Vue+uniapp微信小程序的学生实习与就业管理系统的详细设计和实现
    X86-64 汇编学习1
  • 原文地址:https://blog.csdn.net/weixin_42202992/article/details/133201437