java 调试:到 System.out.println() 或不 System.out.println()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4589623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Debugging: to System.out.println() or to not System.out.println()
提问by James P.
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. Also, what can be considered as good approach(es) to debugging overall?
那是我的问题。更具体地说,我正在尝试习惯 Eclipse 的调试器,我想知道在某些情况下是否仍然可以打印到控制台,或者它是否被认为是应该完全避免的不良做法。另外,什么可以被认为是整体调试的好方法?
回答by user541686
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. It's easier for debugging and also the right way to do it.
System.out.println()
通常重定向到文件或其他输出,而这几乎总是打印在控制台上。调试更容易,也是正确的方法。
Edit (warning: subjective):
编辑(警告:主观):
Since you asked about whether System.out.println
should be entirely avoided: I don't believe in anythingthat you must alwaysavoid, be it using goto's, crashing your computer with a BSOD, or whatever. Sometimes you just need a quick-and-dirty way to get small things done fast, and it just plain isn't worththe 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. Use your judgment when deciding if something should be used or not, but never set rules for yourself like "I'll neveruse goto!". :)
既然您询问是否System.out.println
应该完全避免:我不相信您必须始终避免的任何事情,无论是使用 goto、BSOD 使您的计算机崩溃,还是其他任何事情。有时,您只需要一种快速而肮脏的方法来快速完成小事,而花费 1 小时来尝试以“正确”的方式做事,而不是 5 小时,这显然不值得- 分钟修复,无论“好”方式有多好。在决定是否应该使用某些东西时,请使用您的判断力,但永远不要为自己设定规则,例如“我永远不会使用 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. 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. Sure, you'll reboot, but unless your reboot takes hours, you just saved yourself that much time.
假设您正在调试崩溃的驱动程序,并且您怀疑if
正在执行不应执行的语句。与其花三个小时来找出如何使用ZwRaiseHardError
来显示消息框,不如直接调用KeBugCheck
内部if
并让该死的系统崩溃。当然,您会重新启动,但除非重新启动需要几个小时,否则您只是为自己节省了那么多时间。
回答by Costi Ciudatu
The best choice would be a logging library (of course, this adds an extra dependency to your project). Check out commons-logging, for instance. 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). 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.
最好的选择是日志库(当然,这会为您的项目增加额外的依赖)。例如,查看 commons-logging。主要优点是您可以在 DEBUG 级别编写调试消息,并且在部署代码时,您只需将记录器配置为跳过这些消息(而不是在代码中搜索所有出现的 System.out.println) . 另一个很大的优势是记录器通常可以配置为在任何地方编写(甚至发送电子邮件或 SMS),而无需接触您的代码。
回答by DVK
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
You should generally strive to have as much debugging as possible (ideally using some standard logger like
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)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.Why can it be useful? Among other reasons, because running a Java program in a debugger may be much slower than outside of it; or because your bug manifests in an environment/situation that can't be easily replicated in your Eclipse debugger.
小点:如果您的程序实际上通过 System.out 向控制台输出了一些有用的东西,您可能希望将调试信息打印到
System.err
您通常应该努力进行尽可能多的调试(最好使用一些标准记录器,如
log4j
)。这既可以在您实际开发程序时简化调试,又可以在生产中更轻松地调试已发布的代码。好处是你的代码保持不变,你不需要添加 debugf 打印,但默认情况下,日志配置可以关闭日志,直到真正需要它(或至少降低日志级别)就一般简单的“向
println
墙投掷”调试而言,它有时可能是最快的调试方式之一,尽管它绝不应该是唯一/主要的调试方式。为什么它会有用?除其他原因外,因为在调试器中运行 Java 程序可能比在调试器外运行要慢得多;或者因为您的错误出现在无法在您的 Eclipse 调试器中轻松复制的环境/情况中。
回答by salexander
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. 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. If the debugging print lines are going to stay in your code, then I would advise on using a logging framework like 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. Don't just log everything at INFO.
如果在修复错误后调试打印行不会保留在代码中,那么请执行对您来说最简单的操作。Lambert 建议使用 System.err.println() 是个好主意,因为您可以将它与程序可能产生的其他输出区分开来。如果调试打印行将保留在您的代码中,那么我建议您使用 log4j 之类的日志记录框架。通过这种方式,您可以根据您是尝试调试某些内容还是只是在生产中运行来提高或降低输出级别。使用 log4j 时,请确保以正确的级别输出内容。不要只在 INFO 上记录所有内容。
回答by salexander
I use System.out.println
for my debugging in case i have a problem or to inform me that methods have started to make sure everything has worked properly but when I publish the program I always remove it because it slows down the program.
我System.out.println
用于调试,以防万一我有问题或通知我方法已经开始确保一切正常,但是当我发布程序时,我总是将其删除,因为它会减慢程序的速度。