代码之家  ›  专栏  ›  技术社区  ›  Abhishek Ramachandran

基于条件截断记录器中异常的堆栈跟踪

  •  1
  • Abhishek Ramachandran  · 技术社区  · 7 年前

    我在用 slf4j 在控制台和自定义文件中记录自定义异常及其堆栈跟踪。我遇到了一种情况,我必须截断一些非关键异常的堆栈跟踪。

    使用 this documentation ,我在我的 logback.xml 文件

    <evaluator name="DISPLAY_EX_EVAL">
        <expression>throwable != null &amp;&amp; throwable instanceof com.abc.NonCriticalException</expression>
        </evaluator>
    
        <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
            <encoder>
                <pattern>%-30(%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread]) %-5level
                    %logger{150} -%msg%n%ex{full, DISPLAY_EX_EVAL}
                </pattern>
            </encoder>
        </appender>
    

    但是,上述配置会在记录配置的异常期间删除所有堆栈跟踪。有没有办法记录匹配异常的截断堆栈跟踪(1或2行)?

    0 回复  |  直到 7 年前
        1
  •  2
  •   Michael Gantman    7 年前

    有一个开源库(由我编写)提供智能stacktrace过滤。我在自己的项目中使用它,效果很好。以下是关于图书馆的文章: Open-Source Java Library With Stack Trace Filtering, Silent String Parsing, and Version Comparison .具体查找“段落” 堆栈跟踪噪声滤波器 “。基本上,它会根据您感兴趣的包前缀过滤stacktrace。您可以通过以下方式找到库: Maven artifact 还是在网上 Github (包括源代码和javadoc)。这是 Javadoc .以下是一个例子。假设您的代码位于“包”中 com.plain.* “.所以如果你把前缀设置为” com.plain. “比原来的stacktrace:

    at com.plain.BookService.listBooks()
    at com.plain.BookService$FastClassByCGLIB$e7645040.invoke()
    at net.sf.cglib.proxy.MethodProxy.invoke()
    at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint()
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed()
    at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed()
    at com.plain.LoggingAspect.logging()
    at sun.reflect.NativeMethodAccessorImpl.invoke0()
    at sun.reflect.NativeMethodAccessorImpl.invoke()
    at sun.reflect.DelegatingMethodAccessorImpl.invoke()
    at java.lang.reflect.Method.invoke()
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs()
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod()
    at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke()
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed()
    at org.springframework.aop.interceptor.AbstractTraceInterceptor.invoke()
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed()
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke()
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed()
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke()
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed()
    at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept()
    at com.plain.BookService$EnhancerByCGLIB$7cb147e4.listBooks()
    at com.plain.web.BookController.listBooks()
    

    将替换为:

    at com.plain.BookService.listBooks()
    at com.plain.BookService$FastClassByCGLIB$e7645040.invoke()
    at net.sf.cglib.proxy.MethodProxy.invoke()
    ...
    at com.plain.LoggingAspect.logging()
    at sun.reflect.NativeMethodAccessorImpl.invoke0()
    ...
    at com.plain.BookService$EnhancerByCGLIB$7cb147e4.listBooks()
    at com.plain.web.BookController.listBooks()
    

    该实用程序可以很好地处理整个stacktrace,包括“由”和“被抑制”部分,还可以动态地(从异常本身)或从包含原始stacktrace的字符串进行过滤

        2
  •  0
  •   Raedwald    7 年前

    但你为什么要这么做 截断 还有stacktrace吗?为什么不记录完整的堆栈跟踪呢。毕竟,这是重要而有用的信息。正当

    我猜你在想你必须 log the stacktrace for all exceptions .那是个错误。您应该只为指示异常的异常记录堆栈跟踪 缺陷 在被调用的代码中(意外的异常)。当然,您的程序记录这些堆栈跟踪应该是不常见的,当它记录这些堆栈跟踪时,您将需要 满的 堆栈跟踪可帮助您调试问题。