如何解析日志文件中的异常内容?

时间:2016-08-02 07:30:07

标签: java regex parsing exception logging

这是日志文件的一部分: -

21-Dec-2015 15:49:17.687 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 73146 ms
21-Dec-2015 15:49:26.497 WARNING [http-nio-8080-exec-7] org.apache.catalina.deploy.NamingResourcesImpl.cleanUp Failed to retrieve JNDI naming context for container [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/project]] so no cleanup was performed for that container
 javax.naming.NameNotFoundException: Name [comp/env] is not bound in this Context. Unable to find [comp].
    at org.apache.naming.NamingContext.lookup(NamingContext.java:818)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:166)
    at org.apache.catalina.deploy.NamingResourcesImpl.cleanUp(NamingResourcesImpl.java:993)
    at org.apache.catalina.deploy.NamingResourcesImpl.stopInternal(NamingResourcesImpl.java:975)
    at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:232)
    at org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5492)
    at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:232)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:141)
    at org.apache.catalina.manager.ManagerServlet.start(ManagerServlet.java:1293)
    at org.apache.catalina.manager.HTMLManagerServlet.start(HTMLManagerServlet.java:672)
    at org.apache.catalina.manager.HTMLManagerServlet.doPost(HTMLManagerServlet.java:220)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.filters.CsrfPreventionFilter.doFilter(CsrfPreventionFilter.java:136)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:108)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:614)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:521)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1096)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:674)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)
21-Dec-2015 15:54:57.372 INFO [main] org.apache.catalina.core.StandardServer.await A valid shutdown command was received via the shutdown port. Stopping the Server instance.
21-Dec-2015 15:54:57.373 INFO [main] org.apache.coyote.AbstractProtocol.pause Pausing ProtocolHandler ["http-nio-8080"]
21-Dec-2015 15:54:57.374 INFO [main] org.apache.coyote.AbstractProtocol.pause Pausing ProtocolHandler ["ajp-nio-8009"]
21-Dec-2015 15:54:57.374 INFO [main] org.apache.catalina.core.StandardService.stopInternal Stopping service Catalina

从此我需要仅提取异常但不提取那些INFO消息。所以我尝试过已经建议的方法: -

Pattern headLinePattern = Pattern.compile("([\\w\\.*]+)(:.*)?");
                    Matcher headLineMatcher = headLinePattern.matcher(breader.readLine());
                    if (headLineMatcher.find()) {
                        System.out.println("Headline: " + headLineMatcher.group(1));
                        if (headLineMatcher.group(2) != null) {
                            System.out.println("Optional message "
                                    + headLineMatcher.group(2));

                        }
                    }

                    // "at package.class.method(source.java:123)"
                    Pattern tracePattern = Pattern
                            .compile("\\s*at\\s+([\\w\\.$_]+)\\.([\\w$_]+)(\\(.*)?:(\\d+)\\)(\\n|\\r\\n)");
                    Matcher traceMatcher = tracePattern.matcher(breader.readLine());
                    List<StackTraceElement> stackTrace = new ArrayList<StackTraceElement>();
                    while (traceMatcher.find()) {
                        String className = traceMatcher.group(1);
                        String methodName = traceMatcher.group(2);
                        String sourceFile = traceMatcher.group(3);
                        int lineNum = Integer.parseInt(traceMatcher.group(4));
                        stackTrace.add(new StackTraceElement(className, methodName,
                                sourceFile, lineNum));
                    }
                    System.out.println("Stack: " + stackTrace);

注意:我正在从文件中读取日志。

我将输出(控制台)作为: -

    Headline: 21
Stack: []
Headline: 21
Stack: []
Headline: 21
Stack: []
Headline: 21
Stack: []
Headline: 21
Stack: []
Headline: 21
Stack: []
Headline: 21
Stack: []
Headline: 21
Stack: []
Headline: 21
Stack: []
Headline: 21
Stack: []
Headline: org.apache.catalina.LifecycleException
Optional message : Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/project]]
Stack: []
Headline: at
Stack: []
Headline: at
Stack: []
Headline: at
Stack: []
Headline: Caused
Stack: []
Headline: at
Stack: []
Headline: at
Stack: []
Headline: at
Stack: []
Headline: at
Stack: []
Headline: java.lang.IllegalStateException
Optional message : ContainerBase.addChild: start: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/project]]
Stack: []
Headline: at
Stack: []
Headline: at
Stack: []
Headline: at
Stack: []
Headline: 21
Stack: []
Headline: 21
Stack: []
Headline: 21
Stack: []
Headline: 21
Stack: []
Headline: 21
Stack: []
Headline: 21
Stack: []
Headline: 21
Stack: []
Headline: 21
Stack: []
Headline: 21
Stack: []
Headline: 21
Stack: []
Headline: 21
Stack: []
Headline: javax.naming.NameNotFoundException
Optional message : Name [comp/env] is not bound in this Context. Unable to find [comp].
Stack: []
Headline: at
Stack: []
Headline: at
Stack: []
Headline: at
Stack: []
Headline: at
Stack: []
Headline: at
Stack: []
Headline: at
Stack: []
Headline: at
Stack: []
Headline: at
Stack: []
Headline: at
Stack: []
Headline: at
Stack: []
Headline: at
Stack: []
Headline: at
Stack: []
Headline: at
Stack: []
Headline: 21
Stack: []
Headline: 21
Stack: []
Headline: 21
Stack: []
Headline: javax.naming.NamingException
Optional message : No naming context bound to this class loader
Stack: []
Headline: at
Stack: []
Headline: at
Stack: []
Headline: at
Stack: []
Headline: at
Stack: []
Headline: at
Stack: []
Headline: at
Stack: []
Headline: at
Stack: []
Headline: at
Stack: []
我在做错了吗?我需要做些什么修改?我需要更改正则表达式吗?

0 个答案:

没有答案