bho*_*lis 26 java exception stack-trace
我的Java堆栈跟踪有很多我不关心的条目,显示通过代理和Spring反射方法的方法调用以及类似的东西.它可能很难从我的代码中挑选出实际上来自堆栈跟踪的部分.Ruby on Rails包含一个"堆栈跟踪清理器",您可以在其中指定堆栈跟踪模式的列表,以省略打印的堆栈跟踪 - 对于Java来说,通用的最佳方法是什么?
如果这在任何地方都有效,那将是最好的,包括Eclipse jUnit runner.
Tom*_*icz 15
的IntelliJ-想法允许自定义堆栈跟踪折叠,尤其适用于动态语言.
和分析外部堆栈跟踪工具.
我可以想象一般工具/过滤器在日志框架(如logback或log4j)级别上工作.我不认为对此有任何普遍支持,但我认为实施这一点是个好主意.我会看看,也许这不是那么多工作.
更新:我实现了过滤日志中无关紧要的堆栈跟踪线为的logback,也跟着LBCLASSIC-325.
我实际上写了一个包含几个实用程序的库(https://github.com/michaelgantman/Mgnt/releases/tag/1.01).其中一个是我广泛使用的通用堆栈跟踪过滤器,发现它非常有用.该类称为TextUtils,它具有带有多个重写签名的方法getStacktrace().它需要一个Throwable实例,并允许设置相关包的包前缀.假设您公司的代码始终位于以"com.plain.*"开头的软件包中.所以您设置了这样的前缀并执行此操作
logger.info(TextUtils.getStacktrace(e, true, "com.plain."));
Run Code Online (Sandbox Code Playgroud)
这将非常巧妙地过滤掉跟踪中所有无用的部分,为您提供非常简洁的堆栈跟踪.此外,我发现预先设置前缀非常方便,然后只使用方便的方法
TextUtils.getStacktrace(e);
Run Code Online (Sandbox Code Playgroud)
它也会这样做.要预设前缀,只需使用方法
setRelevantPackage("com.plain.");
Run Code Online (Sandbox Code Playgroud)
此外,如果您使用Spring环境,您可以将以下段添加到Spring配置中,然后全部设置:
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetClass" value="com.mgnt.utils.TextUtils"/>
<property name="targetMethod" value="setRelevantPackage"/>
<property name="arguments" value="com.plain."/>
</bean>
Run Code Online (Sandbox Code Playgroud)
该库附带写得很好(我希望)Javadoc,它详细解释了所有内容.但是这里有一个小预告:你会得到一个跟随堆栈跟踪:
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()
Run Code Online (Sandbox Code Playgroud)
代替
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()
Run Code Online (Sandbox Code Playgroud)