使用sed / awk / whatever从* nix上的日志中获取异常

时间:2014-10-28 10:34:21

标签: java regex bash awk sed


我想从tomcat创建的日志文件中获取异常 是的,我做了一些研究,但因为我对sed或awk没有任何经验 - 调整我发现的东西是有点困难的。 下面显示了一个示例文件,可以使用:

    at org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJobBean.java:86)
Caused by: org.apache.cxf.transport.http.HTTPException: HTTP response '503: Service Temporarily Unavailable' when communicating with http://66.66.66.66:1234/aaa/bbb/ccc
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponseInternal(HTTPConduit.java:1546)
    at org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:62)
    ... 39 more
2014-10-24 11:40:01,558 ERROR [aaa.bbb.ccc.ddd.SomeClass] - some exception on parsing '2007/11/45' bla bla
javax.xml.ws.WebServiceException: Could not send Message.
    at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:145)
    at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:525)
Caused by: org.apache.cxf.transport.http.HTTPException: HTTP response '503: Service Temporarily Unavailable' when communicating with http://66.66.66.66:1234/aaa/bbb/ccc
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponseInternal(HTTPConduit.java:1546)
    at org.apache.cxf.frontend.ClientProxy.invokeSync(ClientProxy.java:88)
    at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:134)
    ... 32 more
2014-10-24 11:40:01,561 ERROR [aaa.bbb.ccc.ddd] - some error with id = 1214
java.lang.NullPointerException
    at sun.reflect.GeneratedMethodAccessor181.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:525)
2014-10-24 11:44:48,253 INFO [org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean] - Closing Hibernate SessionFactory
2014-10-24 11:44:48,253 INFO [org.hibernate.impl.SessionFactoryImpl] - closing
2014-10-24 11:44:48 org.apache.catalina.loader.WebappClassLoader clearThreadLocalMap
2014-10-24 11:44:50 org.apache.coyote.http11.Http11Protocol destroy
INFO: Stopping Coyote HTTP/1.1 on http-8096

我们可以看到:2个全部异常(我们想要它们),1个部分异常(我们不想要它)。为了简化这个例子,我删除了一些重要的东西,比如我们不想要的log4j:ERRORs。

到目前为止我试过了:
AWK(这是我与AWK的第一天,请不要笑:D)。它相当直接。
它在每一行的开头找到“/ t”(制表符)+在+“”(空格)。如果在它之前的2行符合给定条件(例外和日期),它也会打印它们。它工作得很好,但它也会打印出我们不想要的部分异常。

BEGIN {
    preprevious = "";
    previous = "";
}
/^\tat / {
    if( previous != "" ) {
        if(preprevious ~ /20[0-9][0-9]-[0-9][0-9]-[0-9][0-9]/){
            print preprevious;
            preprevious = "";
        }
        if(previous ~ /.*Exception/) {
            print previous;
            previous = "";
        }
    }
    print;
    next;
}
 { preprevious = previous;
    previous = $0; }

我这样跑:

awk -f awkScript testFileExceptions.txt

和BED脚本中的SED(我更喜欢)

#!/bin/sh
if [ "$#" -eq  "2" ]
    then
        tail -n $2 $1 | sed -n "/ ERROR \[/,/20[0-9][0-9]-[0-9][0-9]-[0-9][0-9]/p"
    else
        echo "usage: scriptName filePath amountOfLastLinesInFile"
fi

它匹配'“”+ ERROR +“”'(ERROR两侧都有空格,因此log4j:ERROR将不匹配)到目前为止。它有点工作......
缺点:
1)如果异常之间还有一些额外的界限,它将起作用 - 比如:

        at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:134)
        ... 32 more
2014-10-24 11:40:01,561 INFO [aaa.bbb.ccc.ddd] - AAAAAAAAAAAAAAAAAAAAAAAA
2014-10-24 11:40:01,561 ERROR [aaa.bbb.ccc.ddd] - some error with id = 1214
java.lang.NullPointerException
        at sun.reflect.GeneratedMethodAccessor181.invoke(Unknown Source)

如果没有 - 那么第二个例外将不会显示 2)它还将打印出最后一个匹配的行(具有日期匹配的行)

总而言之,我想要的结果是:

2014-10-24 11:40:01,558 ERROR [aaa.bbb.ccc.ddd.SomeClass] - some exception on parsing '2007/11/45' bla bla javax.xml.ws.WebServiceException: Could not send Message.
        at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:145)
        at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:525) 
Caused by: org.apache.cxf.transport.http.HTTPException: HTTP response '503: Service Temporarily Unavailable' when communicating with http://66.66.66.66:1234/aaa/bbb/ccc
        at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponseInternal(HTTPConduit.java:1546)
        at org.apache.cxf.frontend.ClientProxy.invokeSync(ClientProxy.java:88)
        at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:134)
        ... 32 more

2014-10-24 11:40:01,561 ERROR [aaa.bbb.ccc.ddd] - some error with id = 1214 java.lang.NullPointerException
        at sun.reflect.GeneratedMethodAccessor181.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:525)

我还想将不同的异常保存到不同的文件中(比如说exceptionOutputNNN.txt,例如exceptionOutput001.txt),但我稍后会弄清楚,在我弄清楚如何做到这一点之后不应该那么难对于XML ...:P

好吧......就是这样。我希望有人可以帮助我:) 欢呼声

编辑:请注意,例外可以以“... NN more”和简单的“\ tat org。*”结尾。

2 个答案:

答案 0 :(得分:2)

仍然不确定你想要什么 这应该适用于你想要的输出

 awk '/^[0-9]+/{x=0}/ERROR/{x=1}x' file

输出

2014-10-24 11:40:01,558 ERROR [aaa.bbb.ccc.ddd.SomeClass] - some exception on parsing '2007/11/45' bla bla
javax.xml.ws.WebServiceException: Could not send Message.
    at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:145)
    at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:525)
Caused by: org.apache.cxf.transport.http.HTTPException: HTTP response '503: Service Temporarily     Unavailable' when communicating with http://66.66.66.66:1234/aaa/bbb/ccc
at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponseInternal(HTTPConduit.java:1546)
    at org.apache.cxf.frontend.ClientProxy.invokeSync(ClientProxy.java:88)
    at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:134)
    ... 32 more
2014-10-24 11:40:01,561 ERROR [aaa.bbb.ccc.ddd] - some error with id = 1214
java.lang.NullPointerException
    at sun.reflect.GeneratedMethodAccessor181.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:525)

编辑:对于原始文件

 awk 'a=/^[0-9]+/{x=0}a&&/ERROR/{x=1}x' file

 awk '(/^[0-9]/&&x=/ERROR/)||x' file

答案 1 :(得分:0)

对于将来查看此帖子的任何人,下面是使用Jidders解决方案并将输出保存到文件的脚本(1文件perexception)

#!/bin/sh
if [ "$#" -eq  "2" ]
    then
        rm output/errorOutputFile* 2>/dev/null
        tail -n $2 $1 | awk '(/^[0-9]/&&x=/ERROR/)||x' | awk '/^[0-9]/{g++} { print $0 > "errorOutputFile"g".txt"}' 
    else
        echo "usage: scriptName filePath amountOfLastLinesInFile"
fi

欢呼声