简体   繁体   English

记录和包装异常。 这是好习惯吗?

[英]logging and wrapping exceptions. is this good practice?

Do you think it's worth it to wrap all service methods in a try catch block to log exceptions like this: 您是否认为将所有服务方法包装在try catch块中以记录如下异常是否值得:

public void attachClean(Car instance) {
    log.info("attaching clean Car instance");
    try {
        getSessionFactory().getCurrentSession().lock(instance, LockMode.NONE);
        log.info("attach successful");
    } catch (RuntimeException re) {
        log.error("attach failed", re);
        throw re;
    }
}

it seems like a lot of typing 好像很多打字

You usually log OR rethrow, but not both. 您通常会记录或重新抛出,但不能两者都记录。 Upper layers may be able to handle the exceptional state, and logging entire stacktrace would be unnecessary in such case. 上层可能能够处理异常状态,在这种情况下,无需记录整个stacktrace。 Hovewer, if you absolutely want to make sure it's logged, you can log it on your own. Hovewer,如果您绝对要确保已记录,则可以自行记录。 Logging exception multiple times is better than missing important exception in the log file. 多次记录异常要比在日志文件中丢失重要异常更好。

This is a terrible practice. 这是一个可怕的做法。 You have achieved nothing except more typing, probably duplicated stack trace in logs and few additional lines to test. 除了进行更多的输入,可能在日志中重复的堆栈跟踪以及要测试的其他几行之外,您什么都没有实现。 If this is a RuntimeException and you can't handle it anyhow (logging != handling), just let it fly higher, maybe someone will be able to handle it properly (framework/container preferably).\\ 如果这是RuntimeException并且您无法以任何方式处理它(记录!=处理),只需使其飞得更高,也许有人将能够正确地处理它(最好是框架/容器)。\\

BTW it is a could practice to always log some sort of context on which you work, so you might want to write something like (SLF4J dialect): 顺便说一句,始终记录您正在使用的某种上下文是一种可行的做法,因此您可能想要编写类似(SLF4J方言)的内容:

log.info("attaching clean Car: {}", instance);
getSessionFactory().getCurrentSession().lock(instance, LockMode.NONE);
log.info("{} attach successful", instance);

Think about how the logs will look if everything does this. 考虑一下如果一切都这样做,日志将如何显示。 Then think about how it will look if no one does. 然后考虑一下如果没有人会怎样。

You need a reasonable balance. 您需要一个合理的平衡点。 For instance, if you're creating an API you may want to log things that leave your library into user code. 例如,如果您正在创建API,则可能需要记录将库留在用户代码中的内容。

You can let a dependency injection framework like Spring do the heavy lifting, by configuring it to decorate your classes with AOP proxies . 您可以让Spring这样的依赖注入框架通过配置它来用AOP代理装饰类来完成繁重的工作。 The linked example shows how to use it for authorization checks, but logging would be very similar. 链接的示例显示了如何使用它进行授权检查,但是日志记录非常相似。

In my opinion... NO. 我认为...不。

It is another thing if you are catching the exception and handle the request accordingly, for example, performing somesort of cleanups. 如果您正在捕获异常并相应地处理请求,例如执行某种清理,则是另一回事。 However, if you merely doing a try and catch just to log the error, then it doesn't make sense. 但是,如果您只是尝试捕获并记录错误,则没有任何意义。 After all, the stacktrace from the runtime exception will tell you where the exception occurs. 毕竟,运行时异常中的stacktrace会告诉您异常发生的位置。 You can do a "catch-all" at the very top instead of doing try-catch all over the places. 您可以在最顶部执行“全部捕获”,而不是在所有地方都进行尝试捕获。

If you really paranoid, you can use AspectJ's "around" advice to perform a centralized "try-catch-log-error" block. 如果您确实偏执,可以使用AspectJ的“ around”建议执行集中的“ try-catch-log-error”块。 That is much better than polluting your code with those ugly try catch blocks that do almost nothing. 这比用那些几乎什么都不做的难看的try catch块污染您的代码要好得多。

If your method attachClean is a part of external interface of your system, then logging is cruicial.. Otherwise, you dont have any clue about the exceptions your clients receive.. 如果您的方法attachClean是系统外部接口的一部分,则日志记录至关重要。否则,您对客户端收到的异常一无所知。

If this method is part of internal implementation of your system, logging is unnecessary. 如果此方法是系统内部实现的一部分,则无需进行日志记录。 Because the exception would be handled by your external interface. 因为该异常将由您的外部接口处理。

I wouldn't rethrow the exception. 我不会抛出异常。 You'll have to catch it again, adding more boilerplate code.Also, code inside a try is slower. 您将不得不再次捕获它,添加更多样板代码。尝试中的代码也会更慢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM