Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 11 years ago.
The community reviewed whether to reopen this question 3 years ago and left it closed:
Original close reason(s) were not resolved
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(3)
java.util.logging
使您不必在应用程序中携带多个 jar 文件,并且它与良好的 Formatter 配合使用效果很好。一般来说,在每个类的顶部,您应该:
然后,您可以使用 Logger 类。
对于在执行流顶层进行调试的任何操作,请使用
Level.FINE
:使用
Level.FINER
/Level.FINEST
在循环内部以及在调试基本流程问题时可能并不总是需要查看太多细节的地方:使用日志记录工具的参数化版本来避免生成大量字符串连接垃圾GC 必须跟上。上面的
Object[]
很便宜,通常在堆栈上分配。对于异常处理,始终记录完整的异常详细信息:
我总是将
ex.toString()
作为消息传递到此处,因为当我“grep -n
” for “< code>Exception”在日志文件中,我也可以看到该消息。否则,它将出现在堆栈转储生成的下一行输出中,并且您还必须有一个更高级的正则表达式来匹配该行,这通常会为您提供比您需要查看的更多的输出。java.util.logging
keeps you from having to tote one more jar file around with your application, and it works well with a good Formatter.In general, at the top of every class, you should have:
Then, you can just use various facilities of the Logger class.
Use
Level.FINE
for anything that is debugging at the top level of execution flow:Use
Level.FINER
/Level.FINEST
inside of loops and in places where you may not always need to see that much detail when debugging basic flow issues:Use the parameterized versions of the logging facilities to keep from generating tons of String concatenation garbage that GC will have to keep up with.
Object[]
as above is cheap, on the stack allocation usually.With exception handling, always log the complete exception details:
I always pass
ex.toString()
as the message here, because then when I "grep -n
" for "Exception
" in log files, I can see the message too. Otherwise, it is going to be on the next line of output generated by the stack dump, and you have to have a more advanced RegEx to match that line too, which often gets you more output than you need to look through.应该像这样声明记录器:
所以如果你重构你的类名,它就会遵循。
我写了一篇关于java记录器的文章 示例。
Should declare logger like this:
so if you refactor your class name it follows.
I wrote an article about java logger with examples here.
有很多示例,也有不同类型的日志记录。查看 java.util。日志记录包。
示例代码:
无需对 类进行硬编码名称:
There are many examples and also of different types for logging. Take a look at the java.util.logging package.
Example code:
Without hard-coding the class name: