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));
}
}