Java记录与Log4J

时间:2020-03-05 18:44:16  来源:igfitidea点击:

仍然值得将log4j库添加到Java 5项目中只是为了进行日志记录
假设具有一些很好的过渡设置的文件例外。
还是标准的util.logging工具也可以完成这项工作?

你怎么认为?

解决方案

回答

我想说我们可能对util.logging满足我们所描述的需求。

对于一个好的决策树,看看Log4j vs java.util.logging

Question One :
  Do you anticipate a need for any of the clever handlers that Log4j has that JUL does not have, such as the SMTPHandler, NTEventLogHandler, or any of the very convenient FileHandlers?
  
  Question Two :
  Do you see yourself wanting to frequently switch the format of your logging output? Will you need an easy, flexible way to do so? In other words, do you need Log4j's PatternLayout?
  
  Question Three :
  Do you anticipate a definite need for the ability to change complex logging configurations in your applications, after they are compiled and deployed in a production environment? Does your configuration sound something like, "Severe messages from this class get sent via e-mail to the support guy; severe messages from a subset of classes get logged to a syslog deamon on our server; warning messages from another subset of classes get logged to a file on network drive A; and then all messages from everywhere get logged to a file on network drive B"? And do you see yourself tweaking it every couple of days?
  
  If you can answer yes to any of the above questions, go with Log4j. If you answer a definite no to all of them, JUL will be more than adequate and it's conveniently already included in the SDK.

就是说,这些天几乎所有项目似乎都已经结束,包括log4j,仅仅是因为其他一些库正在使用它。

回答

我会选择log4j。 log4j的可能性根本不会过时!

回答

我建议使用Apache Commmons Logging作为日志接口。这样,我们可以灵活随时随地切换日志记录实现,而无需在端头进行任何代码更改。

回答

我建议我们使用Java的简单日志记录外观(SLF4J)。它支持包括Log4J在内的各种提供程序,并且可以替代Apache Commons Logging。

回答

Log4j已经存在很长时间了,并且运行良好。我没有科学研究来支持它,但是基于我在大量客户中所看到的,很容易发现日志框架比我使用的日志框架要多。它已经存在了很长一段时间,并没有被Next Big Logging Framework取代,后者说明了一些问题。

设置起来很简单,而且基本的添加器(输出)也很容易学习。有一个完整的主机添加程序可用,包括:

  • ConsoleAppender
  • DailyRollingFileAppender
  • ExternallyRolledFileAppender
  • FileAppender
  • JDBCAppender
  • JMSAppender
  • NTEventLogAppender
  • RollingFileAppender
  • SMTPAppender
  • SocketAppender
  • SyslogAppender
  • TelnetAppender
  • WriterAppender

再加上其他。编写自己的添加程序也不难。此外,每个添加程序都具有很大的灵活性,可让我们专门控制日志中的输出。

需要注意的是,除了使用log4j之外,当我使用apache commons日志记录时,我还遇到了一系列的类加载器问题。它仅用于一个特定的应用程序,但是我发现单独使用log4j更简单,而不是像使用commons日志记录这样的抽象层时具有灵活性。

看到这篇文章
更多细节:

祝你好运!

回答

log4j总体来说是一个更好的软件包,并且没有java.util.logging包含的一些小问题。我想直接使用log4j比使用commons日志记录更容易。

回答

java.util.logging提供了一个全面的日志记录程序包,而没有其他一些程序包带来的额外负担。