springboot堆栈跟踪日志记录过滤器仅来自我的包

ax6ht2ek  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(309)

我使用的是带有sl4japi1.7.2的springboot。
有没有办法只从堆栈跟踪中筛选出以我的包开头的行,或者从日志文件中排除来自spring框架或我使用的其他库的行。
这将简化日志文件的读取,并大大减少日志文件所需的空间。在99%以上的情况下,错误都在我的代码中,而不是在我使用的libs中。
我想看看是否有任何统一的解决方案,然后再开始我的新的,自制的补救措施。
这个30多行长的堆栈跟踪

2020-12-18 03:59:40,179 [SimpleAsyncTaskExecutor-2] ERROR RestCommunicatorImpl - sort
com.my.package.exception.AlreadyRunningException: Already running
    at com.my.package.impl.RestCommunicatorImpl.validate(RestCommunicatorImpl.java:244) ~[classes!/:0.6.0-SNAPSHOT]
    at com.my.package.impl.RestCommunicatorImpl.spawn(RestCommunicatorImpl.java:200) ~[classes!/:0.6.0-SNAPSHOT]
    at com.my.package.impl.RestOrchestratorImpl.onAction(RestCommunicatorImpl.java:819) ~[classes!/:0.6.0-SNAPSHOT]
    at com.my.package.impl.RestCommunicatorImpl.sort(RestCommunicatorImpl.java:561) ~[classes!/:0.6.0-SNAPSHOT]
    at com.my.package.impl.RestCommunicatorImpl$$FastClassBySpringCGLIB$$db5641d3.invoke(<generated>) [classes!/:0.6.0-SNAPSHOT]
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) [spring-core-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:738) [spring-aop-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) [spring-aop-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99) [spring-tx-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282) [spring-tx-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) [spring-tx-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) [spring-aop-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:673) [spring-aop-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
    at com.ge.og.rmd.analytics.kernel.boot.orchestrator.impl.RestOrchestratorImpl$$EnhancerBySpringCGLIB$$f68bccb1.sortRuntime(<generated>) [classes!/:0.6.0-SNAPSHOT]
    at com.ge.og.rmd.analytics.kernel.boot.amqp.consumer.ConsumerManager.processCommandOutMessage(ConsumerManager.java:622) [classes!/:0.6.0-SNAPSHOT]
    at sun.reflect.GeneratedMethodAccessor268.invoke(Unknown Source) ~[na:na]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_222]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_222]
    at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:180) [spring-messaging-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
    at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:112) [spring-messaging-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
    at org.springframework.amqp.rabbit.listener.adapter.HandlerAdapter.invoke(HandlerAdapter.java:49) [spring-rabbit-1.7.2.RELEASE.jar!/:na]
    at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.invokeHandler(MessagingMessageListenerAdapter.java:126) [spring-rabbit-1.7.2.RELEASE.jar!/:na]
    at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.onMessage(MessagingMessageListenerAdapter.java:106) [spring-rabbit-1.7.2.RELEASE.jar!/:na]
    at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:822) [spring-rabbit-1.7.2.RELEASE.jar!/:na]
    at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:745) [spring-rabbit-1.7.2.RELEASE.jar!/:na]
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.access$001(SimpleMessageListenerContainer.java:97) [spring-rabbit-1.7.2.RELEASE.jar!/:na]
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$1.invokeListener(SimpleMessageListenerContainer.java:189) [spring-rabbit-1.7.2.RELEASE.jar!/:na]
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.invokeListener(SimpleMessageListenerContainer.java:1276) [spring-rabbit-1.7.2.RELEASE.jar!/:na]
    at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.executeListener(AbstractMessageListenerContainer.java:726) [spring-rabbit-1.7.2.RELEASE.jar!/:na]
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.doReceiveAndExecute(SimpleMessageListenerContainer.java:1219) [spring-rabbit-1.7.2.RELEASE.jar!/:na]
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.receiveAndExecute(SimpleMessageListenerContainer.java:1189) [spring-rabbit-1.7.2.RELEASE.jar!/:na]
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.access$1500(SimpleMessageListenerContainer.java:97) [spring-rabbit-1.7.2.RELEASE.jar!/:na]
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1421) [spring-rabbit-1.7.2.RELEASE.jar!/:na]
    at java.lang.Thread.run(Thread.java:748) [na:1.8.0_222]

应该是7排

2020-12-18 03:59:40,179 [SimpleAsyncTaskExecutor-2] ERROR RestCommunicatorImpl - sort
com.my.package.exception.AlreadyRunningException: Already running
    at com.my.package.impl.RestCommunicatorImpl.validate(RestCommunicatorImpl.java:244) ~[classes!/:0.6.0-SNAPSHOT]
    at com.my.package.impl.RestCommunicatorImpl.spawn(RestCommunicatorImpl.java:200) ~[classes!/:0.6.0-SNAPSHOT]
    at com.my.package.impl.RestOrchestratorImpl.onAction(RestCommunicatorImpl.java:819) ~[classes!/:0.6.0-SNAPSHOT]
    at com.my.package.impl.RestCommunicatorImpl.sort(RestCommunicatorImpl.java:561) ~[classes!/:0.6.0-SNAPSHOT]
    at com.my.package.impl.RestCommunicatorImpl$$FastClassBySpringCGLIB$$db5641d3.invoke(<generated>) [classes!/:0.6.0-SNAPSHOT]

谢谢。

ltskdhd1

ltskdhd11#

有一个开源库mgntutils(由我编写),它完全满足您的要求—智能stacktrace过滤。下面是textutils类的javadoc
你要做的就是:

try {
...
} catch(Exception e) {
  logger.info("your message {}", TextUtils.getStacktrace(e, true, "com.my.package.");
}

如果您在代码中的任何地方只调用一次 TextUtils.setRelevantPackage("com.my.package."); 而不是使用 TextUtils.getStacktrace(e, true, "com.my.package."); 你可以用 TextUtils.getStacktrace(e); . 方法的所有版本 TextUtils.getStacktrace(...) 返回stacktrace作为一个可以被过滤或未过滤的字符串。您可以将库作为maven工件或在github上获取,包括源代码和javadoc。您可以在这里阅读一篇描述这个库的文章:带有堆栈跟踪过滤、静默字符串解析、unicode转换器和版本比较的mgntutils开源java库。请注意,本文描述了库的大部分特性,但不是所有特性

相关问题