简体   繁体   English

调试:到System.out.println()或不到System.out.println()

[英]Debugging: to System.out.println() or to not System.out.println()

That is my question. 这是我的问题。 More specifically, I'm trying to get used to Eclipse's debugger and I'd like to know if printing to console is still done in some cases or if it's considered a bad practise that should be entirely avoided. 更具体地说,我正在尝试习惯Eclipse的调试器,我想知道在某些情况下打印到控制台是否仍然完成,或者它是否被认为是一种应该完全避免的不良做法。 Also, what can be considered as good approach(es) to debugging overall? 还有什么可以被认为是整体调试的好方法?

Use System.err.println() instead. 请改用System.err.println()

Why? 为什么?

System.out.println() is often redirected to a file or another output, while this is pretty much always printed on the console. System.out.println()经常被重定向到一个文件或另一个输出,而这几乎总是打印在控制台上。 It's easier for debugging and also the right way to do it. 它更容易调试,也是正确的方法。


Edit (warning: subjective): 编辑(警告:主观):

Since you asked about whether System.out.println should be entirely avoided: I don't believe in anything that you must always avoid, be it using goto's, crashing your computer with a BSOD, or whatever. 因为你询问是否应该完全避免使用System.out.println :我不相信你必须经常避免的任何事情 ,无论是使用goto,使用BSOD使你的计算机崩溃,还是其他什么。 Sometimes you just need a quick-and-dirty way to get small things done fast, and it just plain isn't worth the 1 hour you'll spend on it to try to do things the "right" way, instead of a 5-minute fix, no matter how good the "good" way is. 有时你只需要一种快速而又肮脏的方式来快速完成小事情,而且很明显,你花费1小时就不值得尝试以“正确”的方式做事,而不是5 - 无论多么好的“好”方式,都能解决问题。 Use your judgment when deciding if something should be used or not, but never set rules for yourself like "I'll never use goto!". 在决定是否应该使用某些东西时使用你的判断,但从不为自己设定规则,比如“我永远不会使用goto!”。 :) :)

Edit 2 (example): 编辑2(示例):

Let's say you're debugging a crashing driver and you suspect that an if statement that shouldn't be execute is being executed. 假设您正在调试崩溃的驱动程序,并且您怀疑正在执行不应执行的if语句。 Instead of spending three hours finding out how to use ZwRaiseHardError to display a message box, just call KeBugCheck inside the if and crash the darned system. 而不是花费三个小时来找出如何使用ZwRaiseHardError来显示消息框,只需在if调用KeBugCheck并使darned系统崩溃。 Sure, you'll reboot, but unless your reboot takes hours, you just saved yourself that much time. 当然,你会重新启动,但除非重启需要几个小时,否则你只需要节省很多时间。

The best choice would be a logging library (of course, this adds an extra dependency to your project). 最好的选择是日志库(当然,这会为您的项目增加额外的依赖性)。 Check out commons-logging, for instance. 例如,查看commons-logging。 The main advantage is that you can write your debug messages in the DEBUG level and when you deploy your code, you'll just configure the logger to skip those messages (instead of searching for all occurrences of System.out.println in your code). 主要优点是您可以在DEBUG级别编写调试消息,并且在部署代码时,您只需将记录器配置为跳过这些消息(而不是在代码中搜索所有出现的System.out.println) 。 One other great advantage is that loggers usually can be configured to write anywhere (even send email messages or SMS) also without touching your code. 另一个很大的优点是记录器通常可以配置为在任何地方写入(甚至发送电子邮件或SMS),而无需触及您的代码。

  1. Minor point: if your program actually outputs something useful to the console via System.out, you may want to instead print the debugging info to System.err 小点:如果你的程序实际上通过System.out向控制台输出了一些有用的东西,你可能想要将调试信息打印到System.err

  2. You should generally strive to have as much debugging as possible (ideally using some standard logger like log4j ). 您通常应该尽可能多地进行调试(理想情况下使用一些标准记录器,如log4j )。 This both eases debugging when you're actually developing the program AND allows for much easier debugging of already-released code in production. 这既可以在您实际开发程序时简化调试,又可以更轻松地调试生产中已发布的代码。 The benefit is that your code remains unchanged and you don't need to ADD debugf prints, yet by default the logging config can turn off the logging until it's actually needed (or at least turn down the level of logs) 好处是你的代码保持不变,你不需要ADD debugf打印,但默认情况下,日志配置可以关闭日志记录,直到实际需要(或至少调低日志级别)

  3. As far as general simple "throw println s at the wall" debugging, it can sometimes be one of the fastest ways to debug, though it should by no means be the only/main one. 至于一般简单的“在墙上抛出println ”调试,它有时可能是最快的调试方式之一,尽管它绝不应该是唯一的/主要的。

    Why can it be useful? 它为什么有用? Among other reasons, because running a Java program in a debugger may be much slower than outside of it; 还有其他原因,因为在调试器中运行Java程序可能比它外部慢得多; or because your bug manifests in an environment/situation that can't be easily replicated in your Eclipse debugger. 或者因为您的错误表现在Eclipse调试器中无法轻松复制的环境/情境中。

If the debugging print lines are not going to stay in the code after you've fixed your bug, then do whatever is easiest for you. 如果在修复bug之后调试打印行不会留在代码中,那么就做最简单的事情。 Lambert's advice of using System.err.println() is a good idea since you can differentiate it from other output that your program may produce. Lambert使用System.err.println()的建议是一个好主意,因为您可以将它与程序可能产生的其他输出区分开来。 If the debugging print lines are going to stay in your code, then I would advise on using a logging framework like log4j. 如果调试打印行将保留在您的代码中,那么我建议使用log4j之类的日志框架。 This way you can dial up or down the level of output based on whether you're trying to debug something or just running in production. 这样,您可以根据您是在尝试调试某些内容还是仅在生产中运行来上下调整输出级别。 Be sure to output things at the right level when using log4j. 使用log4j时,请务必输出正确的级别。 Don't just log everything at INFO. 不要只记录INFO的所有内容。

我使用System.out.println进行调试,以防我遇到问题或通知我方法已经开始确保一切正常,但是当我发布程序时,我总是删除它,因为它会减慢程序的速度。

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

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