Java 日志记录与 Log4J

发布于 2024-07-05 10:09:54 字数 1449 浏览 7 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

莫言歌 2024-07-12 10:09:54

我想说,您可能对 util.logging 满足您所描述的需求感到满意。

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

问题一:
您是否预计需要 Log4j 具有而 JUL 没有的任何巧妙处理程序,例如 SMTPHandler、NTEventLogHandler 或任何非常方便的 FileHandler?

问题二:
您是否认为自己想要频繁切换日志输出的格式? 您需要一种简单、灵活的方法来做到这一点吗? 换句话说,你需要Log4j的PatternLayout吗?

问题三:
在生产环境中编译并部署应用程序后,您是否预计确实需要能够更改应用程序中的复杂日志记录配置? 您的配置听起来是否类似于“来自此类的严重消息通过电子邮件发送给支持人员;来自类子集的严重消息被记录到我们服务器上的系统日志守护进程;来自类的另一个子集的警告消息被记录”到网络驱动器 A 上的文件,然后来自各处的所有消息都记录到网络驱动器 B 上的文件中”? 您是否认为自己每隔几天就会调整一次?

如果您对上述任何一个问题的回答是“是”,请选择 Log4j。 如果您对所有这些问题的答案都是肯定的,那么 JUL 就足够了,而且它已经方便地包含在 SDK 中。

也就是说,现在几乎每个项目似乎都包含了 log4j,哪怕只是因为其他一些库使用了它。

I'd say you're probably fine with util.logging for the needs you describe.

For a good decision tree, have a look at 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.

That said, pretty much every project these days seems to wind up including log4j, if only because some other library uses it.

以往的大感动 2024-07-12 10:09:54

java.util.logging 提供了一个全面的日志记录包,没有其他一些提供的多余包袱。

java.util.logging offers a comprehensive logging package without the excess baggage some of the others provide..

早乙女 2024-07-12 10:09:54

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

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.

梨涡 2024-07-12 10:09:54

我建议使用 Apache Commmons Logging 作为日志记录界面。 这样您就可以随时灵活地切换日志记录实现,而无需更改任何代码。

I recommend using Apache Commmons Logging as your logging interface. That way you have the flexibility to switch logging implementations anytime you want without requiring any code changes on your end.

束缚m 2024-07-12 10:09:54

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

I would go with log4j. The possibilites with log4j is not obsolete at all!

流星番茄 2024-07-12 10:09:54

我建议您使用Simple Logging Facade for Java (SLF4J)。 它支持包括 Log4J 在内的不同提供程序,并且可以用作 Apache Commons Logging 的替代品。

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.

月依秋水 2024-07-12 10:09:54

Log4j 已经存在很长时间了,并且运行得非常好。 我没有科学研究支持它,但根据我在大量客户中看到的情况,它很容易成为我认为使用最多的日志框架。 它已经存在很长时间了,并且没有被下一个大型日志框架取代,这说明了一些事情。

它的设置非常简单,并且很容易学习基本的附加程序(输出)。 有可用的整个主机附加程序,包括:

  1. ConsoleAppender
  2. DailyRollingFileAppender
  3. externallyRolledFileAppender
  4. FileAppender
  5. JDBCAppender
  6. JMSAppender
  7. NTEventLogAppender
  8. RollingFileAppender
  9. SMTPAppender
  10. SocketAppender
  11. SyslogAppender
  12. TelnetAppender
  13. WriterAppender

加上其他。 编写自己的appender 也不难。 此外,每个附加程序都具有很大的灵活性,允许您专门控制日志中的输出内容。

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

请参阅这篇文章
更多详细信息

祝你好运!

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.

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:

  1. ConsoleAppender
  2. DailyRollingFileAppender
  3. ExternallyRolledFileAppender
  4. FileAppender
  5. JDBCAppender
  6. JMSAppender
  7. NTEventLogAppender
  8. RollingFileAppender
  9. SMTPAppender
  10. SocketAppender
  11. SyslogAppender
  12. TelnetAppender
  13. 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.

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.

See this article for
more details:

Good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文