Spring 切换日志系统

本文探讨了Spring框架默认使用commons-logging带来的性能问题,并建议切换到SLF4J,提供Log4J和Logback两种日志系统的切换步骤。通过Gradle或Maven配置,可以实现日志框架的平滑替换,解决类加载问题,提高日志系统效率。
摘要由CSDN通过智能技术生成

为何需要切换

由于历史原因,Spring最开始在core包中引入的是commons-logging(JCL标准实现)的日志系统,官方考虑到兼容问题,在后续的Spring版本中并未予以替换,而是继续沿用。如果考虑到性能、效率,应该自行进行替换,在项目中明确指定使用的日志框架,从而在编译时就指定日志框架。
commons-logging日志系统是基于运行发现算法(常见的方式就是每次使用org.apache.commons.logging.LogFactory.getLogger(xxx),就会启动一次发现流程),获取最适合的日志系统进行日志记录,其效率要低于使用SLF4J,在编译时明确指定日志系统的方式,目前常用的日志框架有logback、log4j、log4j2等。
动态绑定请参考:http://blog.csdn.net/yycdaizi/article/details/8276265

官方说明:
The mandatory logging dependency in Spring is the Jakarta Commons Logging API (JCL). We compile against JCL and we also make JCL Log objects visible for classes that extend the Spring Framework. It’s important to users that all versions of Spring use the same logging library: migration is easy because backwards compatibility is preserved even with applications that extend Spring. The way we do this is to make one of the modules in Spring depend explicitly on commons-logging (the canonical implementation of JCL), and then make all the other modules depend on that at compile time. If you are using Maven for example, and wondering where you picked up the dependency on commons-logging, then it is from Spring and specifically from the central module called spring-core.

Spring中强制使用的是Jakarta Commons Logging API (JCL)日志系统。我们基于JCL进行编译,构建JCL日志对象,这些同时也对扩展自Spring类可见的。对于使用者而言,确保不同版本的Spring使用相同的日志系统是非常重要的–代码迁移需要确保逆向兼容性。我们之所以这样做,是为了在Spring的一个包中明确的依赖于commons-logging(JCL权威实现),而其他包就基于这个包进行构建编译。如果你使用maven,你可以发现commons-logging以来自Spring-core包。

The nice thing about commons-logging is that you don’t need anything else to make your application work. It has a runtime discovery algorithm that looks for other logging frameworks in well known places on the classpath and uses one that it thinks is appropriate (or you can tell it which one if you need to). If nothing else is available you get pretty nice looking logs just from the JDK (java.util.logging or JUL for short). You should find that your Spring application works and logs happily to the console out of the box in most situations, and that’s important.

使用commons-logging的好处是,你不需要做其他额外事情就可以让程序正常工作。它有运行时的发现算法,能够在运行时从classpath自动发现其他日志框架,并自行挑选其中一个合适的,或者你自行指定一个。如果在运行时没有发现任何其他日志框架,则commons-loggin会直接使用JDK的日志系统(java.util.logging或JUL)。

Unfortunately, the runtime discovery algorithm in commons-logging, while convenient for the end-user, is problematic. If we could turn back the clock and start Spring now as a new project it would use a different logging dependency. The first choice would probably be the Simple Logging Facade for Java ( SLF4J), which is also used by a lot of other tools that people use with Spring inside their applications.

非常不幸的是,对于终端用户而言,commons-logging的运行时发现算法是合适的,但对于其他使用场景,却是问题重重。如果时间可以重来,让我们重新选择一个不同的日志系统,我们可能会选择SLF4J。

如何进行操作

以Log4J为例
1. 使用SLF4J-JCL桥接包替换commons-logging包。确保Spring框架使用的logging call能够转换为SLF4J的API
2. 引入SLF4J API包
3. 引入SLF4J-Log4J桥接包,以使得SLF4J使用Log4J进行日志记录
4. 引入Log4J API包

以Logback为例
1. 使用SLF4J-JCL桥接包替换commons-logging包。确保Spring框架使用的logging call能够转换为SLF4J的API
2. 引入SLF4J API包
3. 引入Logback包

官方说明
There are basically two ways to switch off commons-logging:

  • Exclude the dependency from the spring-core module (as it is the only module that explicitly depends on commons-logging)
  • Depend on a special commons-logging dependency that replaces the library with an empty jar

两步骤切换日志系统

  • 从spring-core包中排除掉commons-logging(Spring-core是唯一依赖该包的)
  • 依赖一个特殊的空的commons-logging包

SLF4J is a cleaner dependency and more efficient at runtime than commons-logging because it uses compile-time bindings instead of runtime discovery of the other logging frameworks it integrates. This also means that you have to be more explicit about what you

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值