Java 日志与 Log4J
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31840/
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
Java Logging vs Log4J
提问by Okami
Is it still worth to add the log4j library to a Java 5 project just to log let's say some exceptions to a file with some nice rollover settings. Or will the standard util.logging facility do the job as well?
是否仍然值得将 log4j 库添加到 Java 5 项目只是为了记录让我们说一些具有一些不错的翻转设置的文件的异常。或者标准的 util.logging 工具也能完成这项工作?
What do you think?
你怎么认为?
回答by Matt Sheppard
I'd say you're probably fine with util.logging for the needs you describe.
我会说你可能对 util.logging 满足你描述的需求没问题。
For a good decision tree, have a look at Log4j vs java.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 具有而 JUL 没有的任何智能处理程序,例如 SMTPHandler、NTEventLogHandler 或任何非常方便的 FileHandlers?
问题二:您是否看到自己想要频繁切换日志输出的格式?您是否需要一种简单、灵活的方法来做到这一点?换句话说,你需要Log4j的PatternLayout吗?
问题三:在应用程序在生产环境中编译和部署之后,您是否预计确实需要能够更改应用程序中复杂的日志配置?您的配置是否听起来像“来自此类的严重消息通过电子邮件发送给支持人员;来自类子集的严重消息被记录到我们服务器上的系统日志守护进程;来自另一个类子集的警告消息被记录到网络驱动器 A 上的文件;然后来自任何地方的所有消息都记录到网络驱动器 B 上的文件中”?你是否看到自己每隔几天就调整一次?
如果您对上述任何问题的回答是肯定的,请使用 Log4j。如果您对所有这些都回答肯定的“否”,则 JUL 将绰绰有余,并且它已经方便地包含在 SDK 中。
That said, pretty much every project these days seems to wind up including log4j, if only because some other library uses it.
也就是说,现在几乎每个项目似乎都包含 log4j,即使只是因为其他库使用它。
回答by svrist
I would go with log4j. The possibilites with log4j is not obsolete at all!
我会和 log4j 一起去。log4j 的可能性根本没有过时!
回答by Edmund Tay
I recommend using Apache Commmons Loggingas your logging interface. That way you have the flexibility to switch logging implementations anytime you want without requiring any code changes on your end.
我建议使用Apache Commmons Logging作为您的日志记录界面。这样您就可以随时灵活地切换日志记录实现,而无需更改任何代码。
回答by Brian Matthews
I recommend that you use the Simple Logging Facade for Java(SLF4J). It supports different providers that include Log4J and can be used as a replacement for Apache Commons Logging.
我建议您使用Simple Logging Facade for Java(SLF4J)。它支持包括 Log4J 在内的不同提供程序,并可用作 Apache Commons Logging 的替代品。
回答by Rydell
Log4j has been around for a long time, and it works very well. I have no scientific study to back it, but based on what I've seen at a large number of clients, it is easily the logging framework that I see used more than any other. It has been around for a long time, and not been replaced by the Next Big Logging Framework, which says something.
Log4j 已经存在很长时间了,并且运行良好。我没有任何科学研究来支持它,但根据我在大量客户中看到的情况,我发现它很容易成为我看到使用最多的日志记录框架。它已经存在很长时间了,并没有被 Next Big Logging Framework 所取代,这说明了什么。
It is dead simple to set up, and easy to learn the basic appenders (outputs). There are a whole host appenders that are available, including:
设置非常简单,并且易于学习基本的附加程序(输出)。有一个完整的主机附加程序可用,包括:
- ConsoleAppender
- DailyRollingFileAppender
- ExternallyRolledFileAppender
- FileAppender
- JDBCAppender
- JMSAppender
- NTEventLogAppender
- RollingFileAppender
- SMTPAppender
- SocketAppender
- SyslogAppender
- TelnetAppender
- WriterAppender
- 控制台Appender
- DailyRollingFileAppender
- 外部滚动文件附加程序
- 文件附加程序
- JDBCAppender
- JMSAppender
- NTEventLogAppender
- 滚动文件附加程序
- SMTPAppender
- SocketAppender
- SyslogAppender
- TelnetAppender
- WriterAppender
Plus others. It isn't difficult to write your own appender either. Additionally there is a great deal of flexibility in each of the appenders that allow you to control specifically what is output in your log.
加上其他人。编写自己的 appender 也不难。此外,每个附加程序都有很大的灵活性,允许您具体控制日志中的输出内容。
One note, I had a series of classloader problems when I used apache commons logging in addition to log4j. It was only for one specific application, but I found it simpler to use log4j alone, rather than to have the flexibility offered when using an abstraction layer like commons logging.
需要注意的是,除了 log4j 之外,当我使用 apache commons 日志记录时,我遇到了一系列类加载器问题。它仅适用于一个特定的应用程序,但我发现单独使用 log4j 比使用公共日志记录等抽象层时提供的灵活性更简单。
See this article for more details:
有关更多详细信息,请参阅这篇文章 :
Good luck!
祝你好运!
回答by Will Sargent
log4j is a much nicer package overall, and doesn't have some of the hiccups that java.util.logging contains. I'd second that using log4j directly is easier than using the commons logging.
log4j 总体上是一个更好的包,并且没有 java.util.logging 包含的一些小问题。我认为直接使用 log4j 比使用公共日志记录更容易。
回答by adam
java.util.logging offers a comprehensive logging package without the excess baggage some of the others provide..
java.util.logging 提供了一个全面的日志记录包,没有其他一些提供的额外负担。