gpt4 book ai didi

java - 仅来 self 的包的 Spring Boot 堆栈跟踪日志记录过滤器

转载 作者:行者123 更新时间:2023-12-04 15:09:44 24 4
gpt4 key购买 nike

我将 Springboot 与 sl4j-api 1.7.2 结合使用。

有没有办法从堆栈跟踪中过滤掉以我的包开头的行,或者从日志文件中排除来自 spring 框架或我使用的其他库的行。

这将简化日志文件的读取,并极大地减少日志文件所需的空间。在超过 99% 的情况下,错误都在我的代码中,而不是在我使用的库中。

在开始我自己的自制补救措施之前,我想看看是否有任何统一的解决方案。

例如。这 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]

谢谢。

最佳答案

有一个开源库 MgntUtils(由我编写)可以完全满足您的要求 - 智能堆栈跟踪过滤。这是 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(...) 方法的所有版本都将堆栈跟踪作为可能已过滤或未过滤的字符串返回。您可以将图书馆作为 maven artifacts 获取或在 Github 上包括源代码和 Javadoc。您可以在此处阅读描述该库的文章:MgntUtils Open Source Java library with stack trace filtering, Silent String parsing, Unicode converter and Version comparison .请注意,该文章描述了该库的大部分而非全部功能

关于java - 仅来 self 的包的 Spring Boot 堆栈跟踪日志记录过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65436457/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com