printStackTrace

时间:2017-05-11 06:47:50

标签: java

我用春天。我有许多捆绑链接依赖。

mvc bundle - > (来电) biz bundle - > 核心包 - > 普通包 - > 远程RPC服务

所有捆绑包都加载在同一个JVM中。但是 mvc bundle 并没有直接调用 biz bundle 的对象,而是 biz bundle 通过公开接口来发布服务到 mvc bundle mvc bundle 有一个代理对象,代理对象调用服务实现对象。

我的stacktrace看起来像这样:

...
at com.mycorp.kbdatacenter.common.service.integration.nomo.NomoDataServiceClientImpl.getPropertyDistribution(NomoDataServiceClientImpl.java:64) ~[kbdatacenter-common-service-integration-1.0.0.jar:?]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_92]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_92]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_92]
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_92]
at com.mycorp.sofa.runtime.service.binding.JvmBindingAdapter$JvmServiceInvoker$1.call(JvmBindingAdapter.java:87) ~[?:?]
at com.mycorp.ambush.chain.codewrapper.CodeWrapperInvocationImpl.proceed(CodeWrapperInvocationImpl.java:77) ~[ambush-1.0.10.jar:?]
at com.mycorp.guardian.client.sofa.GuardianCodeWrapperInterceptor.invokeCodeWrapper(GuardianCodeWrapperInterceptor.java:76) ~[guardian-sofa-1.1.5.jar:?]
at com.mycorp.ambush.chain.codewrapper.CodeWrapperInvocationImpl.proceed(CodeWrapperInvocationImpl.java:71) ~[ambush-1.0.10.jar:?]
at com.mycorp.ambush.catalog.AbstractCatalog.doIntercept(AbstractCatalog.java:252) ~[ambush-1.0.10.jar:?]
at com.mycorp.ambush.api.AmbushRPCUtil.invokeClient(AmbushRPCUtil.java:79) ~[ambush-1.0.10.jar:?]
at com.mycorp.sofa.runtime.service.binding.JvmBindingAdapter$JvmServiceInvoker.doInvoke(JvmBindingAdapter.java:90) ~[?:?]
at com.mycorp.sofa.runtime.service.component.ServiceProxy.invoke(ServiceProxy.java:29) ~[?:?]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213) ~[spring-aop-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at com.sun.proxy.$Proxy91.getPropertyDistribution(Unknown Source) ~[?:?]
at com.mycorp.kbdatacenter.core.service.tag.TagDataQueryServiceImpl.queryByFilterRule(TagDataQueryServiceImpl.java:105) ~[kbdatacenter-core-service-1.0.0.jar:?]
... (119 lines more)

我的核心软件包有一个代理对象 nomoDataServiceClient ,它在公共软件包中调用 Impl 对象。

我的问题是,是否可以过滤StackTraceElement [],忽略包 com.sun.proxy sun.reflect 等。我希望我的堆栈看起来像这样干净:

...
at com.mycorp.kbdatacenter.common.service.integration.nomo.NomoDataServiceClientImpl.getPropertyDistribution(NomoDataServiceClientImpl.java:64) ~[kbdatacenter-common-service-integration-1.0.0.jar:?]
at com.mycorp.kbdatacenter.core.service.tag.TagDataQueryServiceImpl.queryByFilterRule(TagDataQueryServiceImpl.java:105) ~[kbdatacenter-core-service-1.0.0.jar:?]
... 

2 个答案:

答案 0 :(得分:2)

是的,你可以。如果捕获异常,则可以获取堆栈跟踪元素,从中创建新的(已过滤的)数组,并覆盖异常的堆栈跟踪,例如

try {
  //your exception throwing call here
} catch(Exception e) {
  e.setStackTrace(Arrays.stream(e.getStackTrace())
                        .filter(se -> !se.getClassName().startsWith("com.sun.proxy")) //or any other filter criteria
                        .collect(toList())
                        .toArray(new StackTraceElement[0])); //create a new stacktrace from the filtered list
  //either e.printStackStrace();
  //or rethrow
}

答案 1 :(得分:0)

实际上我写了一个开源java库,它完全符合你的要求。它可以作为maven工件和git存储库使用,因此您可以将其用作第三方库或自行获取代码。基本上,如果您将其用作第三方库,则需要将前缀设置为" com.mycorp。"然后只使用方法TextUtils.getStacktrace(e),它将为您过滤掉堆栈跟踪作为字符串。以下是对文章的引用,该文章解释了获取库的位置以及如何使用它。 Open Source Java library with stack trace filtering, Silent String parsing Unicode converter and Version comparison。请参阅段落" Stacktrace噪音过滤器"这个库很容易使用。这是pom文件的一个片段,它将maven依赖项保存到库中:

    <dependency>
        <groupId>com.github.michaelgantman</groupId>
        <artifactId>MgntUtils</artifactId>
        <version>1.04</version> 
</dependency>

    <dependency>
        <groupId>com.github.michaelgantman</groupId>
        <artifactId>MgntUtils</artifactId>
        <version>1.04</version>
        <classifier>javadoc</classifier> 
</dependency>

    <dependency>
        <groupId>com.github.michaelgantman</groupId>
        <artifactId>MgntUtils</artifactId>
        <version>1.04</version>
        <classifier>sources</classifier> 
</dependency>