简体   繁体   English

Spring框架中的java logging vs Log4j。 哪一个是最合适的

[英]java logging vs Log4j in Spring framework. Which one is the most suitable

We are developing a web-based application in Java using the Spring framework. 我们正在使用Spring框架在Java中开发基于Web的应用程序。 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. 我们想知道哪个Logging系统最适合它,无论是Log4j还是JUL(java.util.Logging),它与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. 就我而言,前者在开发人员中更受欢迎,并提供更高的自定义选项,但我不确定哪个更容易适应spring。

any help will be appreciated. 任何帮助将不胜感激。

thanks! 谢谢!

Before you start with log4j, look at logback . 在开始使用log4j之前,请查看logback Log4j shouldn't be used for new projects anymore. Log4j不应再用于新项目。 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). 如果您有遗留代码需要任何日志记录框架,请使用slf4j使旧代码与logback进行对话(如果必须,则使用log4j)。

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. 您不应该使用JUL的主要原因是它实现了日志记录所需的最低限度,配置很难,如果您使用任何不使用它的组件,您将不得不处理两个日志记录构架。

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. 无论你在自己的代码中使用哪种日志框架,我都记得,Spring对公共日志记录有很强的依赖性。 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. 您仍然可以使用您喜欢的任何内容进行自己的日志记录(我建议使用SLF4J作为您的外观,并使用logback作为您的实现),但Spring内部需要公共日志记录。 Whether you want to depend on multiple frameworks is your choice; 您是否想要依赖多个框架是您的选择; it shouldn't prove problematic. 它不应该证明是有问题的。

Spring uses commons logging, which is a log abstraction facility but has issues that sl4j doesn't have. Spring使用commons日志记录,这是一个日志抽象工具,但有sl4j没有的问题。 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. Hibernate转移到了slf4j - 我希望看到spring也这样做,但我不认为他们在这方面有任何计划。

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. 因此,只要你的日志抽象工具(使用slf4j)记录到你在春天配置公共登录的同一个日志框架,那么你就是好的。 One log configuration for both. 一个日志配置。 You might find an adapter for logback for commons and slf4j. 您可能会找到适用于commons和slf4j的logback的适配器。

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. 首先,java.util.logging很糟糕。 It's a pain to use, and a pain to configure. 这是一个痛苦的使用,并配置的痛苦。 Log4J is nicer on both counts. Log4J在两个方面都更好。 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. Spring使用commons-logging,反过来将使用log4j(如果在类路径上找到)或java.util.logging。 In otherwords, if your application already has log4j present, then Spring will effectively use that, and so should you. 换句话说,如果您的应用程序已经存在log4j,那么Spring将有效地使用它,您也应该如此。 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. 如果log4j尚不存在,则使用java.util.logging(如果选择),或者将log4j添加到类路径并使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM