使用 java.util.logging 和 Log4j 记录器的区别

发布于 2024-08-21 08:59:43 字数 119 浏览 7 评论 0原文

我正在开发一个java应用程序,我必须使用日志记录机制。现在我很困惑是选择 java 库记录器还是选择 Log4j 记录器。

所以我想知道什么时候可以使用java记录器 当我可以使用 log4j 记录器时。

I am developing a java application for which i have to use a logging mechanism. And now i am confused to choose either java libraries logger or to go for Log4j logger.

So i want to know when i can go for java logger
and when i can go for log4j logger.

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

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

发布评论

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

评论(5

半枫 2024-08-28 08:59:43

我建议您使用 SLF4J 来将您的应用程序与特定的日志框架分离。它具有适用于各种流行日志记录框架的适配器,例如 Jakarta Logging、JDK1.4 日志记录、log4j 等,使其成为满足日志记录需求的良好抽象。

I'd suggest you go with SLF4J instead to decouple your application from specific logging frameworks. It has adapters for various popular logging frameworks such as Jakarta Logging, JDK1.4 logging, log4j etc. making it a good abstraction for logging needs.

马蹄踏│碎落叶 2024-08-28 08:59:43

Logger 类早期并不是 jdk 的一部分,因此出现了几个库实现。 Log4j 库拥有最全面的日志记录实用程序集之一(Formatters、Appenders 等)。然而,对于大多数开发人员来说,这太过分了,简单的 java.util.Logger 就足够了。

我个人在记录器实现上使用自定义包装器。这使我能够定义自定义调用来执行功能日志记录/审核。

Logger class was not part of jdk earlier on, so several library implementations sprung up. The Log4j library has one of the most comprehensive set of logging utilities (Formatters, Appenders etc). However, for most developers this would be an overkill and the simple java.util.Logger would suffice.

I personally use a custom wrapper over my logger implementation. This enables me to define custom calls to carry out functional logging/auditing.

小ぇ时光︴ 2024-08-28 08:59:43

Apache Commoms Logging 项目和 SLF4J,其中任何一个都抽象了底层日志库。

在实践中,我倾向于使用 Log4J 而不是内置的日志记录类。主要是因为 Log4J 可以在应用程序服务器中针对每个 Web 应用程序进行配置,而 JDK 日志记录是针对每个 JVM 进行配置。

There are the Apache Commoms Logging project and SLF4J, either of which abstracts the underlying logging library.

In practice I tend to use Log4J over the built in logging classes. Mainly because Log4J can be configured per web-app in an application server, whereas JDK logging is configured per JVM.

月牙弯弯 2024-08-28 08:59:43

我目前推荐的方法是使用 SLF4J 作为日志记录 API。然后,您可以根据您发现的需求来选择日志记录框架。

我写了一篇关于我认为开始使用 SLF4J 的最佳实践的文章,以及当前放置的一个简单的“登录到 System.out”。 http://runjva.appspot.com/logging101/index.html

希望是有帮助。

The approach I would currently recommend is to use SLF4J as the logging API. You can then pick your logging framework depending on your needs as you discover them.

I did a writeup on what I consider to be best practice in getting started with SLF4J and a simple "log to System.out" which is currently placed at. http://runjva.appspot.com/logging101/index.html

Hopefully it is helpful.

墨小墨 2024-08-28 08:59:43

我发现 Log4j 在调整日志记录 cfg 时更加灵活,而无需在生产环境中重新编译代码。

I find Log4j more flexible when it comes to tweaking the logging cfg without re-compiling code in production environment.

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