如何从java日志文件中提取异常?

时间:2012-02-16 09:33:15

标签: bash parsing logging trace

我有一个包含很多异常的巨大日志文件,我需要在它之前和之后提取完整的堆栈跟踪和几行。如果这个工具是bash脚本,那将是完美的。 例如:

    $16.02.2012 16:04:34 *INFO * [main] InitialContextInitializer: Reference bound: rmirepository (InitialContextInitializer.java, line 203) 
16.02.2012 16:04:34 *ERROR* [main] StandaloneContainerInitializedListener: Error of StandaloneContainer initialization (StandaloneContainerInitializedListener.java, line 109) 
java.lang.RuntimeException: Cannot instantiate component key=org.exoplatform.services.jcr.ext.script.groovy.GroovyScript2RestLoader type=org.exoplatform.services.jcr.ext.script.groovy.GroovyScript2RestLoader found at file:/home/roman/reports/backup/1.14.7-5636/rdbms/single/exo-tomcat_1.14.7-5636/exo-configuration.xml
    at org.exoplatform.container.jmx.MX4JComponentAdapter.getComponentInstance(MX4JComponentAdapter.java:134)
    at org.exoplatform.container.management.ManageableComponentAdapter.getComponentInstance(ManageableComponentAdapter.java:68)
    at org.exoplatform.container.ConcurrentPicoContainer.getInstance(ConcurrentPicoContainer.java:468)
    at org.exoplatform.container.ConcurrentPicoContainer.getComponentInstancesOfType(ConcurrentPicoContainer.java:366)
    at org.exoplatform.container.CachingContainer.getComponentInstancesOfType(CachingContainer.java:111)
    at org.exoplatform.container.LifecycleVisitor.visitContainer(LifecycleVisitor.java:151)
    at org.exoplatform.container.ConcurrentPicoContainer.accept(ConcurrentPicoContainer.java:615)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.picocontainer.defaults.AbstractPicoVisitor.traverse(AbstractPicoVisitor.java:32)
    at org.exoplatform.container.LifecycleVisitor.traverse(LifecycleVisitor.java:90)
    at org.exoplatform.container.LifecycleVisitor.start(LifecycleVisitor.java:170)
    at org.exoplatform.container.ConcurrentPicoContainer.start(ConcurrentPicoContainer.java:554)
    at org.exoplatform.container.ExoContainer.start(ExoContainer.java:266)
    at org.exoplatform.container.StandaloneContainer$3.run(StandaloneContainer.java:178)
    at org.exoplatform.container.StandaloneContainer$3.run(StandaloneContainer.java:175)
    at org.exoplatform.commons.utils.SecurityHelper.doPrivilegedAction(SecurityHelper.java:291)
    at org.exoplatform.container.StandaloneContainer.getInstance(StandaloneContainer.java:174)
    at org.exoplatform.container.StandaloneContainer.getInstance(StandaloneContainer.java:129)
    at org.exoplatform.ws.frameworks.servlet.StandaloneContainerInitializedListener.contextInitialized(StandaloneContainerInitializedListener.java:104)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4205)
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4704)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:799)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:779)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:601)
    at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:675)
    at org.apache.catalina.startup.HostConfig.deployDescriptors(HostConfig.java:601)
    at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:502)
    at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1315)
    at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:324)
    at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:142)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1061)
    at org.apache.catalina.core.StandardHost.start(StandardHost.java:840)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
    at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463)
    at org.apache.catalina.core.StandardService.start(StandardService.java:525)
    at org.apache.catalina.core.StandardServer.start(StandardServer.java:754)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:595)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
Caused by: java.lang.RuntimeException: Cannot instantiate component key=org.exoplatform.services.jcr.RepositoryService type=org.exoplatform.services.jcr.impl.RepositoryServiceImpl found at file:/home/roman/reports/backup/1.14.7-5636/rdbms/single/exo-tomcat_1.14.7-5636/exo-configuration.xml
    at org.exoplatform.container.jmx.MX4JComponentAdapter.getComponentInstance(MX4JComponentAdapter.java:134)
    at org.exoplatform.container.management.ManageableComponentAdapter.getComponentInstance(ManageableComponentAdapter.java:68)
    at org.exoplatform.container.ConcurrentPicoContainer.getInstance(ConcurrentPicoContainer.java:468)
    at org.exoplatform.container.ConcurrentPicoContainer.getComponentInstanceOfType(ConcurrentPicoContainer.java:422)
    at org.exoplatform.container.CachingContainer.getComponentInstanceOfType(CachingContainer.java:139)
    at org.exoplatform.container.ExoContainer.createComponent(ExoContainer.java:407)
    at org.exoplatform.container.jmx.MX4JComponentAdapter.getComponentInstance(MX4JComponentAdapter.java:96)
    ... 45 more

4 个答案:

答案 0 :(得分:5)

使用awk

BEGIN {
    previous = "";
}
/^\tat / {
    if( previous != "" ) {
        print previous;
    previous = "";
    }
    print;
    next;
}
 { previous = $0; }

应该做的伎俩。简而言之,查找模式\tat(tab,at,blank),它几乎总是在堆栈跟踪中使用。

如果你有很多例外,那么你可以使用map(AWK的术语中的关联数组)来保存部分异常消息,然后进行统计(比如最常发生的异常)。

答案 1 :(得分:1)

Java异常块中的大部分行都以TAB分隔符开头。

因此,要从巨大的日志文件中获取所有异常,您可以使用grep TAB分隔符。

使用以下命令来grep文件中的所有异常:

**$ grep -P -A 15 -B 15 "^\t" inputLogFile**

答案 2 :(得分:0)

您可以使用以下命令 grep -B 10 -A 100 "Caused By" app.log --> 这将给出记录在应用程序日志文件中的所有 java 异常。如果要查找特定异常,可以更改字符串。 https://www.techiedelight.com/java-program-search-exceptions-huge-log-file-on-server/

答案 3 :(得分:-1)

您可以尝试这样的事情,

=======
grep -B20 -A20 ^'$16.02.2012 16:04:34' <path/to/log/file>
=======

Where 
-B20 -- will display 20 lines before the line where the match was found.
-A20 -- will display 20 lines after the line where the match was found.