我刚刚登陆一个新的代码库。在努力使应用程序在本地运行时,我注意到我只是在日志和控制台输出中得到错误的单行代码 - 没有堆栈跟踪。我开始搜索代码,唯一能找到记录的是异常消息。 “链接”异常也是如此:仅保留消息。我向该项目的唯一开发商提到了这一点,并询问这是否是一个有意的设计决定。部分答复是“我认为我们不会从[记录堆栈跟踪]中获得太多好处。”
从多年的 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?
发布评论
评论(3)
在代码库中故意创建一个错误并让他追踪它:-)。说真的,如果你能证明它在追踪错误时有多大帮助以及节省了多少时间,你就有了很好的论据。
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.
也许 Google 的 Java 编码标准会被认为足够权威:
Maybe Google's coding standards for Java will be considered authoritative enough:
我发现这个项目中恰好使用了 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.