rubular 中的多行 java 异常正则表达式
Multiline java exception regex in 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(<generated>) ~[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.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(<generated>) ~[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$e6baa6.invoke(<generated>) ~[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 正则表达式与完整堆栈跟踪不匹配。如有任何帮助,我们将不胜感激。
您可以使用
^(?<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)
以匹配换行字符,它是 Ruby 仅 m
标志的行为)
.*
太贪心了,使用惰性量词,.*?
- 为了只匹配日期,使用了前瞻,
(?=\g<time>|\Z)
。它检查当前位置之后是否有时间模式(\g<time>
,递归 time
组模式的子例程)或字符串结尾(\Z
)。
我也"prettified" [^\s]
作为\S
,[\d]
作为\d
,[\s]
作为\s
,并分组重复的模式。您也可以用 \s+
替换空格,以确保匹配任何数量的任何空白字符。
我在应用程序中有这类日志,我想解析它们。我已经创建了一个正则表达式,但它没有按预期工作,因为它没有解析日志中的完整堆栈跟踪。这是我当前的表达方式:
^(?<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(<generated>) ~[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.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(<generated>) ~[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$e6baa6.invoke(<generated>) ~[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 正则表达式与完整堆栈跟踪不匹配。如有任何帮助,我们将不胜感激。
您可以使用
^(?<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)
以匹配换行字符,它是 Ruby 仅m
标志的行为).*
太贪心了,使用惰性量词,.*?
- 为了只匹配日期,使用了前瞻,
(?=\g<time>|\Z)
。它检查当前位置之后是否有时间模式(\g<time>
,递归time
组模式的子例程)或字符串结尾(\Z
)。
我也"prettified" [^\s]
作为\S
,[\d]
作为\d
,[\s]
作为\s
,并分组重复的模式。您也可以用 \s+
替换空格,以确保匹配任何数量的任何空白字符。