Cleaning noise out of Java stack traces

后端 未结 6 1506
感情败类
感情败类 2020-12-15 17:15

My Java stack traces have a lot of entries that I don\'t care about, showing method invocation going through proxies and Spring reflection methods and stuff like that. It ca

6条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-15 17:53

    I actually wrote an open source library MgntUtils (available at Github and maven central) that contains several utilities. Here is a link to an article about the library: MgntUtils Open Source Java library. One of the utilities is a general purpose stacktrace filter that I used extensively and found it very useful. The class is called TextUtils and it has method getStacktrace() with several overridden signatures. It takes a Throwable instance and allows to set a package prefix of the packages that are relevant. Let's say your company's code always resides in packages that start with "com.plain.*" So you set such a prefix and do this

    logger.info(TextUtils.getStacktrace(e, true, "com.plain."));
    

    this will filter out very smartly all the useless parts of the trace leaving you with very concise stacktrace. Also I found it very convinient to pre-set the prefix and then just use the convinience method

    TextUtils.getStacktrace(e);
    

    It will do the same. To preset the prefix just use method

    setRelevantPackage("com.plain.");
    

    Also if you use Spring environment you can add the following segment to your Spring configuration and then you all set:

    
      
      
      
    
    

    The library comes with well written (I hope) Javadoc that explains everything in detail. But here is a little teaser: you will get a following stacktrace:

    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()
    

    instead of

    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()
    

提交回复
热议问题