0

I need to match Java stack trace if it is a stack trace like this one

Exception in thread "main" java.lang.IllegalStateException: A book has a null property
        at com.example.myproject.Author.getBookIds(Author.java:38)
        at com.example.myproject.Bootstrap.main(Bootstrap.java:14)
Caused by: java.lang.NullPointerException
        at com.example.myproject.Book.getId(Book.java:22)
        at com.example.myproject.Author.getBookIds(Author.java:36)

I can match it with ^[[:space:]]+(at|\.{3})\b|^Caused by: The problem is i need to match the entire stack trace and the traces i get look like this:

2019-06-14 15:07:08,142 ThreadPoolAsyncTaskExecutor::Thread 65 ERROR bamboo_user 906x78989x1 vg7ahz 192.168.181.28 /rest/plugins/1.0/bg.nemetschek.jsd.advance-customer-reports-key [o.e.g.b.e.i.dependencies.startup.DependencyWaiterApplicationContextExecutor] Unable to create application context for [bg.nemetschek.jsd.advance-customer-reports], unsatisfied dependencies: none
org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from URL [bundle://293.0:0/META-INF/spring/atlassian-plugins-component-imports.xml]; nested exception is java.lang.IllegalStateException: The bundle is uninstalled.
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:414)
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:336)
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304)
        at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:181)
        at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:217)
        at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188)
        at org.eclipse.gemini.blueprint.context.support.OsgiBundleXmlApplicationContext.loadBeanDefinitions(OsgiBundleXmlApplicationContext.java:170)
        at org.eclipse.gemini.blueprint.context.support.OsgiBundleXmlApplicationContext.loadBeanDefinitions(OsgiBundleXmlApplicationContext.java:140)
        at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:129)
        at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:609)
        at org.eclipse.gemini.blueprint.context.support.AbstractDelegatedExecutionApplicationContext.access$800(AbstractDelegatedExecutionApplicationContext.java:60)
        at org.eclipse.gemini.blueprint.context.support.AbstractDelegatedExecutionApplicationContext$3.run(AbstractDelegatedExecutionApplicationContext.java:242)
        at org.eclipse.gemini.blueprint.util.internal.PrivilegedUtils.executeWithCustomTCCL(PrivilegedUtils.java:85)
        at org.eclipse.gemini.blueprint.context.support.AbstractDelegatedExecutionApplicationContext.startRefresh(AbstractDelegatedExecutionApplicationContext.java:220)
        at org.eclipse.gemini.blueprint.extender.internal.dependencies.startup.DependencyWaiterApplicationContextExecutor.stageOne(DependencyWaiterApplicationContextExecutor.java:224)
        at org.eclipse.gemini.blueprint.extender.internal.dependencies.startup.DependencyWaiterApplicationContextExecutor.refresh(DependencyWaiterApplicationContextExecutor.java:177)
        at org.eclipse.gemini.blueprint.context.support.AbstractDelegatedExecutionApplicationContext.refresh(AbstractDelegatedExecutionApplicationContext.java:157)
        at org.eclipse.gemini.blueprint.extender.internal.activator.LifecycleManager$1.run(LifecycleManager.java:207)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.IllegalStateException: The bundle is uninstalled.
        at org.apache.felix.framework.Felix.getBundleResources(Felix.java:1624)
        at org.apache.felix.framework.BundleImpl.getResources(BundleImpl.java:661)
        at org.eclipse.gemini.blueprint.util.BundleDelegatingClassLoader.getResources(BundleDelegatingClassLoader.java:185)
        at org.springframework.core.io.support.PropertiesLoaderUtils.loadAllProperties(PropertiesLoaderUtils.java:177)
        at org.springframework.beans.factory.xml.DefaultNamespaceHandlerResolver.getHandlerMappings(DefaultNamespaceHandlerResolver.java:155)
        at org.eclipse.gemini.blueprint.extender.internal.support.NamespacePlugins$Plugin.resolve(NamespacePlugins.java:77)
        at org.eclipse.gemini.blueprint.extender.internal.support.NamespacePlugins$5.operate(NamespacePlugins.java:209)
        at org.eclipse.gemini.blueprint.extender.internal.support.NamespacePlugins$5.operate(NamespacePlugins.java:205)
        at org.eclipse.gemini.blueprint.extender.internal.support.LazyBundleRegistry.apply(LazyBundleRegistry.java:159)
        at org.eclipse.gemini.blueprint.extender.internal.support.NamespacePlugins.doResolve(NamespacePlugins.java:205)
        at org.eclipse.gemini.blueprint.extender.internal.support.NamespacePlugins.resolve(NamespacePlugins.java:169)
        ... 2 filtered
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.eclipse.gemini.blueprint.context.support.TrackingUtil$OsgiServiceHandler.invoke(TrackingUtil.java:106)
        at com.sun.proxy.$Proxy605.resolve(Unknown Source)
        at org.eclipse.gemini.blueprint.context.support.DelegatedNamespaceHandlerResolver.resolve(DelegatedNamespaceHandlerResolver.java:55)
        at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1406)
        at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1401)
        at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:168)
        at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.doRegisterBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:138)
        at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:94)
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:508)
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:392)
        ... 20 more

The actual problem is that ... 20 more or ... 2 filtered is not getting matched and i cant match the entire trace so i can take it as one string.

10
  • 4
    Overall, this is just an bad idea. If you need to be informed about probable exceptions, add that functionality in your code instead of scanning log files Commented Jun 14, 2019 at 13:10
  • i am not scanning the files, i need the log for ELK stack. It needs to be visible in Kibana so devs can see the stack traces there because the environment is not accessible from their workstations. Also some of the exceptions are from Jira/Confluence, i need them also, and i cant touch the code there Commented Jun 14, 2019 at 13:11
  • Still sounds like you should pass any exception on when it pops up in the code instead of trying to match log files Commented Jun 14, 2019 at 13:13
  • I cant do this with proprietary Atlassian products....most of stack traces are indeed coming from Jira and/or third party plugin installed on Jira. I have no power there. Commented Jun 14, 2019 at 13:15
  • If you can any method from within your code, then its your job to handle that exception. Any 3rd party library throws their exceptions instead of handling them on their own. Commented Jun 14, 2019 at 13:17

2 Answers 2

2

One option might be to match from a string starting with a date. Then repeat all lines that do not start with a date or have at. Then make sure to match at least 1 line with at and then match all lines untill the next date:

^\d{4}-\d{2}-\d{2}.*(?:\n(?!(?:\d{4}-\d{2}-\d{2}|\h+at)).*)*\n\h+at\h+.*(?:\n(?!\d{4}-\d{2}-\d{2}).*)*

Explanation

  • ^ Start of string
  • \d{4}-\d{2}-\d{2}.* Match a date like pattern followed by 0+ times any char except new line
  • (?: Non capturing group
    • \n(?!(?:\d{4}-\d{2}-\d{2}|\h+at)).* match a newline, then assert what is on the right is not a datelike pattern or 1+ horizontal whitespace chars followed by at. If that is the case, match 0+ times any char except a newline
  • )* Close non capturing group and repeat 0+ times
  • \n\h+at\h+.* Match a line starting with at surrounded by 1+ horizontal whitespace chars
  • (?:\n(?!\d{4}-\d{2}-\d{2}).*)* Match all lines not starting with a datelike pattern

Regex demo

Note in Java to double escape the backslash:

String regex = "^\\d{4}-\\d{2}-\\d{2}.*(?:\\n(?!(?:\\d{4}-\\d{2}-\\d{2}|\\h+at)).*)*\\n\\h+at\\h+.*(?:\\n(?!\\d{4}-\\d{2}-\\d{2}).*)*";
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks man! I will test this on the server and will let you know how it goes :)
@fixxxera, is it possible for the log file to contain log entries other than exception?
yes, it will for sure. Every entry will start with timestamp like this 2019-06-14 14:25:26,567
Thanks a lot. Just tested and it's working as needed.
You can use multiline.match in filebeat for harvesrting mulitline messages like stack traces. Refer this link for details - elastic.co/guide/en/beats/filebeat/current/…
|
1

Try Regex: ^(?:Caused by:|\s+(?:at|\.{3})).*$

Demo

4 Comments

Thank you! This however does not match the first two lines. It does match all text after The bundle is uninstalled. I need all lines as one message.
I assumed you were only interested in the stacktrace (starting with at). Is it guaranteed that the exception details always start with yyyy-mm-dd?
It is guaranteed. The message will always look like this.
I've tested this ^[[:space:]]+(at|\.{3})\b|^(?:Caused by:|\s+(?:at|\.{3})).*$ and it works without the two lines at the start (timestamp lines and beginning of the exception )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.