1

I've these kind of logs in an application and I want to parse them. I've created a regex but it doesn't work as expected as it doesn't parse the full stack trace in the log. Here's my current expression:

^(?<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>.*)

These the logs that I get from my application:

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(..)

You can see here that the regex doesn't match the full stack trace. Any help here would be appreciated.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Waseem Hassan
  • 233
  • 2
  • 10

1 Answers1

4

You may use

^(?<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)

See the regex demo.

The main points are:

  • . is replaced with [\s\S] (or, you may keep ., but use (?m) at the start of the pattern for . to match line break chars, it is a Ruby only behavior of m flag)
  • .* is too greedy, use a lazy quantifier, .*?
  • To only match up to the date, a lookahead is used, (?=\g<time>|\Z). It checks if there is a time pattern (\g<time>, a subroutine that recurses the time group pattern) or end of string (\Z) right after the current location.

I also "prettified" [^\s] as \S, [\d] as \d, [\s] as \s, and grouped repetitive patterns. You may also replace spaces with \s+ to make sure any amount of any whitespace chars are matched.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563