多行Java异常正则表达式

时间:2018-06-26 09:18:38

标签: regex spring logging fluentd rubular

我在应用程序中有这些日志,我想解析它们。我创建了一个正则表达式,但是它不能按预期运行,因为它无法解析日志中的完整堆栈跟踪。这是我当前的表情:

^(?<time>[0-9]+-[0-9]+-[0-9]+\s+[0-9]+:[0-9]+:[0-9]+.[0-9]+)[\s]*(?<level>[^\s]+) (?<pid>[\d]+) --- \[(?<thread>.*)\] (?<class>[^\s]+)[\s]*:[\s]*(?<message>.*)

这些是我从应用程序中获得的日志:

2018-06-26 09:01:42.144 DEBUG 1 --- [io-9090-exec-10] c.stakater.gateway.logging.MethodLogger  : Method called: StackService.fetchStack(..)
2018-06-26 09:01:42.149 ERROR 1 --- [io-9090-exec-10] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.RuntimeException: Stack with ID: 166a998f-dbb2-44a3-be86-asd Not Found!] with root cause
 java.lang.RuntimeException: Stack with ID: 166a998f-dbb2-44a3-be86-asd Not Found!
    at com.stakater.gateway.repository.StackRepositoryService.findById(StackRepositoryService.java:29) ~[classes!/:1.0.16]
    at com.stakater.gateway.repository.StackRepositoryService$$FastClassBySpringCGLIB$$fe7f2cc7.invoke(&lt;generated&gt;) ~[classes!/:1.0.16]
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) ~[spring-core-4.3.12.RELEASE.jar!/:4.3.12.RELEASE]
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:738) ~[spring-aop-4.3.12.RELEASE.jar!/:4.3.12.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) ~[spring-aop-4.3.12.RELEASE.jar!/:4.3.12.RELEASE]
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99) ~[spring-tx-4.3.12.RELEASE.jar!/:4.3.12.RELEASE]
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282) ~[spring-tx-4.3.12.RELEASE.jar!/:4.3.12.RELEASE]
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) ~[spring-tx-4.3.12.RELEASE.jar!/:4.3.12.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.12.RELEASE.jar!/:4.3.12.RELEASE]
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:673) ~[spring-aop-4.3.12.RELEASE.jar!/:4.3.12.RELEASE]
    at com.stakater.gateway.repository.StackRepositoryService$$EnhancerBySpringCGLIB$$ddd17ba6.findById(&lt;generated&gt;) ~[classes!/:1.0.16]
    at com.stakater.gateway.service.StackService.fetchStack(StackService.java:118) ~[classes!/:1.0.16]
    at com.stakater.gateway.service.StackService$$FastClassBySpringCGLIB$$97e6baa6.invoke(&lt;generated&gt;) ~[classes!/:1.0.16]
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) ~[spring-core-4.3.12.RELEASE.jar!/:4.3.12.RELEASE]
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:738) ~[spring-aop-4.3.12.RELEASE.jar!/:4.3.12.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) ~[spring-aop-4.3.12.RELEASE.jar!/:4.3.12.RELEASE]
    at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:52) ~[spring-aop-4.3.12.RELEASE.jar!/:4.3.12.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.12.RELEASE.jar!/:4.3.12.RELEASE]
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) ~[spring-aop-4.3.12.RELEASE.jar!/:4.3.12.RELEASE]
2018-06-26 09:01:42.144 DEBUG 1 --- [io-9090-exec-10] c.stakater.gateway.logging.MethodLogger  : Method called: StackService.fetchStack(..)

您可以看到here,该正则表达式与完整堆栈跟踪不匹配。任何帮助,我们将不胜感激。

1 个答案:

答案 0 :(得分:3)

您可以使用

^(?<time>\d+(?:-\d+){2}\s+\d+(?::\d+){2}\.\d+)\s*(?<level>\S+) (?<pid>\d+) --- \[(?<thread>[\s\S]*?)\] (?<class>\S+)\s*:\s*(?<message>[\s\S]*?)(?=\g<time>|\Z)

请参见regex demo

要点是:

  • .[\s\S]替换(或者,您可以保留.,但是在模式的开头使用(?m)来使.与行匹配打破字符,这是m标志的仅Ruby行为)
  • .*太贪婪,请使用惰性量词.*?
  • 要仅匹配日期,请使用前瞻(?=\g<time>|\Z)。它检查在当前位置之后是否有时间模式(\g<time>,这是递归time组模式的子例程)或字符串结尾(\Z)。

我也将[^\s]“美化”为\S,将[\d]命名为\d,将[\s]命名为\s,并对重复的模式进行分组。您也可以用\s+替换空格,以确保匹配任意数量的空白字符。