有哪些资源可以让开发人员相信 Java 堆栈跟踪值得记录

发布于 2024-10-18 14:31:13 字数 497 浏览 2 评论 0 原文

我刚刚登陆一个新的代码库。在努力使应用程序在本地运行时,我注意到我只是在日志和控制台输出中得到错误的单行代码 - 没有堆栈跟踪。我开始搜索代码,唯一能找到记录的是异常消息。 “链接”异常也是如此:仅保留消息。我向该项目的唯一开发商提到了这一点,并询问这是否是一个有意的设计决定。部分答复是“我认为我们不会从[记录堆栈跟踪]中获得太多好处。”

从多年的 Java 开发经验中,我知道堆栈跟踪有多么有价值:它们告诉您异常发生在哪一行,并且您可以跟踪如何到达那里。您阅读了堆栈跟踪和相关代码,这可能足以解决您遇到的 80%(注意:统计数据没有统计支持)的错误。

我不是在寻找关于记录什么或何时记录的争论 - 这不是问题,并且它已在其他地方介绍过。真的,这对我来说似乎是一个非常基本的事情,除了“轨迹告诉你错误在哪里”之外,我从来没有想过如何解释它。我希望能够快速找到来自 Effective Java 等的引用,但 Google 到目前为止还没有找到任何内容。

有没有权威的、令人信服的来源来描述为什么应该关注堆栈跟踪?

I've just landed in a new code base. While working to make the application run locally, I noticed I was just getting one-liners for errors in the log and console output - no stack traces. I started searching code and the only thing I could find logged was the exception message. Ditto for "chaining" exceptions: only the message was being retained. I mentioned this to the project's sole developer and asked if it was an intentional design decision. The reply was, in part, "I don't think we'd gain much from [logging stack traces]."

From years of Java development, I know how valuable stack traces are: they tell you which line the exception happened on and you can track how you got there. You read the stack trace and the relevant code and that's probably enough to solve 80% (N.B.: statistic has no statistical backing) of errors you encounter.

I'm not looking for arguments about what to log or when - that's not the question, and it's been covered elsewhere. Really, this just seems like such a fundamental thing to me that I never thought of how to explain it other than that "strack traces show you where the error was." I was hoping I could quickly find a quote from Effective Java or the like, but Google hasn't turned up anything so far.

Any suggestions of authoritative, convincing sources for descriptions of why you should pay attention to stack traces?

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

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

发布评论

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

评论(3

落花浅忆 2024-10-25 14:31:13

在代码库中故意创建一个错误并让他追踪它:-)。说真的,如果你能证明它在追踪错误时有多大帮助以及节省了多少时间,你就有了很好的论据。

Create an intentional error in the code base and make him track it down :-). Seriously, you have good arguments, if you could just demonstrate how much it helps and how much time it saves when tracking down a bug.

神经大条 2024-10-25 14:31:13

也许 Google 的 Java 编码标准会被认为足够权威:

异常所有方法都必须声明
他们的非运行时异常
可以抛出他们的 throws 子句。如果
他们可能会扔几个,但都必须是
列出,尽管将列表缩短了
列出基类而不是全部
他们的孩子受到鼓励,只要
信息不会丢失。在
特别是,“抛出异常”应该
永远不会出现在任何方法中,除了
极端情况,例如方法
是如此通用,以至于它们实际上可能
抛出任何异常。

类似地,'catch(异常e)'
不应该在任何地方使用,因为它
可能会捕获不应该捕获的异常
被抓住,从而隐藏问题。这
正在捕获的确切异常
应该拼写出来。

出于类似的原因,空捕获
应避免使用块(除了
测试出现异常的代码
预期),因为他们也可能隐藏
问题并使调试变得困难。
如果您知道异常可以
永远不会发生总是提出
OntopiaRuntimeException 带有消息
就像 '"(IMPOSSIBLE) " + e', 并通过
在捕获的异常中保留
堆栈跟踪。

Maybe Google's coding standards for Java will be considered authoritative enough:

Exceptions All methods must declare
the non-runtime-exceptions that they
may throw in their throws clause. If
they may throw several, all must be
listed, though shortening the list by
listing base classes instead of all
their children is encouraged, provided
information is not lost. In
particular, 'throws Exception' should
never appear on any method, except in
extreme cases, such as methods that
are so generic that they may in fact
throw any exception.

Similarly, 'catch (Exception e)'
should not be used anywhere, since it
may catch exceptions that should not
be caught and thus hide problems. The
exact exceptions that are being caught
should be spelled out instead.

For similar reasons, empty catch
blocks should be shunned (except in
testing code where exceptions are
expected), since they may also hide
problems and make debugging difficult.
If you know that the exception can
never happen always raise an
OntopiaRuntimeException with a message
like '"(IMPOSSIBLE) " + e', and pass
in the caught exception to preserve
the stack trace.

拍不死你 2024-10-25 14:31:13

我发现这个项目中恰好使用了 Apache Commons Logging,它有这些 记录异常的指南指定“使用完整堆栈跟踪进行记录”。

我仍然希望找到一些非常直白地表明这应该是默认行为的东西,除非你有一个非常非常好的理由不这样做。详细说明为什么这是一个好主意会更好。

I found that Apache Commons Logging, which happens to be used on this project, has these guidelines for logging exceptions that specify "logged with full stack trace."

I would still like to find something that states quite bluntly that this should be the default behavior unless you have a really, really good reason to do otherwise. Going into detail on why it is a good idea would be even better.

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