在 Java 中在异常和日志之间切换?

发布于 2024-11-15 15:30:45 字数 87 浏览 3 评论 0原文

我在一个项目中,我们想在测试期间抛出异常,但在生产中只是记录异常而不是抛出它,因为我们不想停止执行。有人建议 Spring 可能有办法使其可配置。有什么建议吗?

I'm on a project where we would like to throw an exception during test, but in production just log the exception instead of throwing it, b/c we don't want to stop execution. Someone suggested Spring might have a way to make this configurable. Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

拥抱我好吗 2024-11-22 15:30:45

您可以在生产环境中使用方面方法。您需要向您的服务类提供适当的建议,以捕获异常并将其记录到系统日志中。下面的代码是根据 EJB 规范编写的,但是在 Spring 中它看起来非常相似:

public class CatchExceptionInterceptor{
     private static final Logger log = LoggerFactory.getLogger(CatchExceptionInterceptor.class);
     @AroundInvoke
     public Object invoke(InvocationContext ic){
          try{
                return ic.proceed();
          }catch (Exception e){
                log.error("Error while invoking business method "+e.getMessage(),e);
                return ???// null or whatevery you like
          }
     }
}

You may use aspect approach in production environment. You need to advise your service class with the appropriate advice which would catch the exception and log it into the syslog. Below code is written according to EJB spec however in Spring it will look like quite similar:

public class CatchExceptionInterceptor{
     private static final Logger log = LoggerFactory.getLogger(CatchExceptionInterceptor.class);
     @AroundInvoke
     public Object invoke(InvocationContext ic){
          try{
                return ic.proceed();
          }catch (Exception e){
                log.error("Error while invoking business method "+e.getMessage(),e);
                return ???// null or whatevery you like
          }
     }
}
层林尽染 2024-11-22 15:30:45

基本上,我认为您不应该更改生产和测试之间的异常处理等基本内容。你总是会产生副作用,例如在生产中记录异常可能会产生另一个异常等。
然而,我自己也遇到过这样的问题,我总是通过使用中央异常处理程序来解决它们,我可以将其配置为记录消息或重新抛出它们,或两者兼而有之(不推荐,但产品经理希望这样做,直到他们看到没有人可以再阅读日志了;))。

try {
   doSomethingDangerous();
} catch(Exception e) {
   exceptionHandler.handle(e);
}

当然,您可以充分发挥您的想象力,了解如何获取异常处理程序、您需要多少种不同类型的异常处理程序、它们如何处理异常等。您还可以为handle() 或其他任何内容提供自定义消息。
问题是,如果您将其集中化,则可以在一个地方配置行为。

Basically, i don't think you should change essential things like exception handling between production and test. You will always have side-effects, e.g. logging the an exception in production can produce another exception etc.
However, i have had problems like this myself, and i always solved them by using a central exception Handler, which i could configure to either log messages or rethrow them, or both (not recommended, but product managers want this, until they see that no one can read the logs anymore ;) ).

try {
   doSomethingDangerous();
} catch(Exception e) {
   exceptionHandler.handle(e);
}

Of course, you can use all your imagination on how to get the exceptionHandler, how many different kinds of them you need, what they do with the exception etc. You could also supply a custom message to handle(), or wathever.
The thing is, if you centralize this, you can configure the behaviour in one place.

赴月观长安 2024-11-22 15:30:45

如果没有太多上下文,很难给出建议,但我认为您会想要像 AspectJ 这样的东西(它确实与 Spring 很好地集成)。

在这种情况下,您将创建附加到所有相关方法调用的方面,捕获底层异常,并决定重新抛出或记录它。这可能是 @Around 建议。

然而,我质疑这种方法的优点,因为如果异常是可恢复的,则无需在开发环境和生产环境之间做任何不同的事情。它只会使测试变得复杂。

Without much context it's hard to give an advice, but I think you would want to have something like AspectJ (which really nicely integrates with Spring).

In this case, you would create aspect that will attach to all relevant method calls, catch underlying exception, and decide to rethrow or to log it. It would probably be @Around advice.

I , however, question merrits of such approach as if exception is recoverable, there is no need to do anything different between dev and prod environments. It will only complicate testing.

滥情稳全场 2024-11-22 15:30:45

正如其他答案所解释的那样,您尝试做的事情是有问题的(主要是因为您的异常处理程序是软件的一部分,并且应该像在生产中运行一样进行测试)。也就是说,您正在寻找的内容看起来更像断言

断言通常仅在测试中启用,但不适用于生产。 Java 中有多种选项,内置的 assert 或一些第三方库。

What you are trying to do is problematic, as explained by the other answers (mainly because your exception handlers are part of the software, and should be tested like they run in production). That said, what you are looking for looks more like assertions.

Assertions are commonly enabled in testing only, but not for production. There are several options in Java, the built-in assert, or some 3rd-party library.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文