Spring 框架中的 java 日志记录与 Log4j。哪一款最合适

发布于 2024-08-02 14:04:10 字数 187 浏览 6 评论 0原文

我们正在使用 Spring 框架用 Java 开发一个基于 Web 的应用程序。我们想知道哪种日志系统最适合它,是 Log4j 还是与 jdk 集成的 JUL (java.util.Logging)。就我而言,前者更受开发人员欢迎,并提供更高的定制选项,但我不确定哪个更容易与 spring 适配。

任何帮助将不胜感激。

谢谢!

We are developing a web-based application in Java using the Spring framework. We are wondering which Logging system would be the most appropriate for it, whether Log4j or JUL (java.util.Logging), which is integrated with jdk. As far as I'm concerned, the former is more popular among developers and offers higher customization options, but I'm not sure which is simpler to adapt with spring.

any help will be appreciated.

thanks!

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

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

发布评论

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

评论(4

葬シ愛 2024-08-09 14:04:10

在开始使用 log4j 之前,请先查看 logback。 Log4j 不应该再用于新项目。如果您有需要任何日志记录框架的遗留代码,请使用 slf4j 使旧代码与 logback (或 log4j如果你必须的话)。

您不应该使用 JUL 的主要原因是它实现了日志记录所需的最低限度,配置充其量是困难的,如果您使用任何不使用它的组件,您将不得不处理两个日志记录框架。

Before you start with log4j, look at logback. Log4j shouldn't be used for new projects anymore. If you have legacy code that needs any logging framework, use slf4j to make the old code talk to logback (or log4j if you must).

The main reasons you should not use JUL is that it implements the bare minimum that you need for logging, the configuration is hard at best and, if you use any component that doesn't use it, you'll have to deal with two logging frameworks.

自在安然 2024-08-09 14:04:10

无论您在自己的代码中使用哪种日志记录框架,据我所知,Spring 都对公共日志记录有很强的依赖性。您仍然可以使用您喜欢的任何内容来进行自己的日志记录(我建议使用 SLF4J 作为您的外观,使用 logback 作为您的实现),但 Spring 内部需要公共日志记录。是否要依赖多个框架是你的选择;这应该不会有问题。

Regardless of which logging framework you use in your own code, as far as I can remember, Spring has a hard dependency on commons-logging. You can still use whatever you like for your own logging (I'd recommend SLF4J as your facade, with logback as your implementation), but Spring internally needs commons-logging. Whether you want to depend on multiple frameworks is your choice; it shouldn't prove problematic.

怪我入戏太深 2024-08-09 14:04:10

Spring 使用公共日志记录,这是一种日志抽象工具,但存在 sl4j 没有的问题。 Hibernate 迁移到 slf4j - 我希望看到 spring 也这样做,但我认为他们在这方面没有任何计划。

因此,只要您的日志抽象工具(使用 slf4j)记录到与您在 spring 中配置的公共日志记录相同的日志记录框架,那么您就可以了。两者的一种日志配置。您可能会找到一个用于 commons 和 slf4j 的 logback 适配器。

Spring uses commons logging, which is a log abstraction facility but has issues that sl4j doesn't have. Hibernate moved to slf4j - I would like to see spring do the same, but I don't think they have any plans in this regard.

So as long as your log abstraction facility (use slf4j) logs to the same logging framework as you've configured commons logging in spring, then you're good. One log configuration for both. You might find an adapter for logback for commons and slf4j.

儭儭莪哋寶赑 2024-08-09 14:04:10

由于其他答案并没有真正回答你的问题,我想我会尝试一下。

首先,java.util.logging 很糟糕。使用起来很痛苦,配置也很痛苦。 Log4J 在这两方面都更好。但是,您应该选择最适合您的一种。 Spring 使用 commons-logging,而后者又会使用 log4j(如果在类路径中找到)或 java.util.logging。换句话说,如果您的应用程序已经存在 log4j,那么 Spring 将有效地使用它,您也应该如此。如果 log4j 尚不存在,则使用 java.util.logging(如果您选择),或者将 log4j 添加到您的类路径并使用它。

Since the other responses didn't actually answer your question, I thought I'd have a stab at it.

Firstly, java.util.logging is horrible. It's a pain to use, and a pain to configure. Log4J is nicer on both counts. However, you should pick whichever one you're most comfortable with. Spring uses commons-logging, which in turn will use either log4j (if found on the classpath) or java.util.logging otherwise. In otherwords, if your application already has log4j present, then Spring will effectively use that, and so should you. If log4j is not already present, then either use java.util.logging (if you choose to), or add log4j to your classpath and use that.

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