log4J的使用级别[英] Usage of log4J levels



在编码时使用Log4J级别的最佳实践是什么? 我的意思是我们什么时候使用信息记录,什么时候使用调试记录/错误记录等.





  • 调试:低水平的东西.缓存命中,缓存错过,打开DB连接
  • 信息:具有业务意义的事件 - 创建客户,收取CC ...
  • 警告:可能是一个问题,但不会阻止您的应用程序.找不到电子邮件地址/无效
  • 错误:意外问题.无法打开DB连接.等...


我的基准总是是 info 级别等于system.out.

debug - 在这里,您将所有跟踪信息,特别是您不想看到"舒适级别"的信息.

info - 将此用于一般消息,进度消息,与应用程序相关的消息,但不用于跟踪.

警告 - 提供警报,即出现问题,或者可能出乎意料,或者使用解决方法,但是该应用程序仍然可以继续(套接字超时/重新撤回,无效的用户输入等).<<<<<<<<<<<<<<<<<<

错误 - 警报有关阻止您应用正常继续进行的问题,例如数据库正在下降,缺少Bootstrap配置.

编写库时的一个常见错误是使用错误级别来指示通话应用程序(使用库的代码)的问题,而不是指示库本身内的实际错误.例如,请参见此Hibernate Bug-> /Hibernate/浏览/HHH-3731


全部 - 我真的不使用它,它实际上与调试或跟踪相同.



What is the best practice in using log4j levels while coding. I mean when do we use INFO logging, when do we use DEBUG logging / ERROR logging etc.


The best way to learn is by example. Read source of some open source things, like, oh, Tomcat or whatever is in your application area, and see what you see.


In general, I follow these guidelines:

  • DEBUG : low level stuff. cache hit, cache miss, opening db connection
  • INFO : events that have business meaning - creating customer, charging cc...
  • WARN : could be a problem but doesn't stop your app. email address not found / invalid
  • ERROR : unexpected problem. couldn't open db connection. etc...


My baseline is always that INFO level is equivalent to System.out, and ERROR is equivalent to System.err.

DEBUG - Here you put all your tracing information, and specifically information that you don't want to see when your "comfort level" is system.out.

INFO - use this one for general messages, progress messages, for application related messages, but not for tracing.

WARN - provide alerts that something is wrong, perhaps unexpected, or that a workaround is used, but the application can still continue (socket timeout/retries, invalid user input, etc.).

ERROR - alerts about problems that prevent your app from continuing normally, e.g. database is down, missing bootstrap configuration.

A common mistake when writing libraries is to use ERROR level to indicate problems with the calling application (the code that uses the library) instead of indicating real errors within the library itself. See for example this hibernate bug -> http://opensource.atlassian.com/projects/hibernate/browse/HHH-3731

This is really annoying because messages from ERROR level are really difficult to suppress, so use them only to indicate problems with your own code.

All - I don't really use this one, it is practically the same as DEBUG or TRACE.