当前位置: 首页> 财经> 访谈 > Spring5 的日志学习

Spring5 的日志学习

时间:2025/7/10 7:35:47来源:https://blog.csdn.net/weixin_42286658/article/details/140876299 浏览次数:0次

我们在使用 Spring5 的过程中会出现这样的现像,就是 Spring5 内部代码打印的日志和我们自己的业务代码打印日志使用的不是统一日志实现,尤其是在项目启动的时候,Spring5 的内部日志使用的是 log4j2,但是业务代码打印使用的可能是 logback ,log4j1 或者 其他日志实现

一、Spring4 采用日志是 commons-logging

二、Spring5 内部日志

1.Spring5 内部日志原理


Spring5 自己在 commons-logging 基础上进行修改,将修改完的代码单独封装为一个 jar 包,Spring5 内部就使用这个jar,我们可以注意一下当我们导入 Spring5 的相关 jar 包的时候,maven会为我们自动依赖一个 spring-jcl 的jar包,这就是那个jar包。

 

 

 可以看出这这两 jar 包的包名都起的一模一样,只是LogFactory的实现是Spring5自己实现的。

2. Spring5 日志关键源码分析

org.apache.commons.logging.LogFactory#getLog(java.lang.String)

	/*** Convenience method to return a named logger.* @param name logical name of the <code>Log</code> instance to be returned*/public static Log getLog(String name) {return LogAdapter.createLog(name);}

org.apache.commons.logging.LogAdapter#createLog

    private static final String LOG4J_SPI = "org.apache.logging.log4j.spi.ExtendedLogger";private static final String LOG4J_SLF4J_PROVIDER =         "org.apache.logging.slf4j.SLF4JProvider";private static final String SLF4J_SPI = "org.slf4j.spi.LocationAwareLogger";private static final String SLF4J_API = "org.slf4j.Logger";private static final LogApi logApi;static {
//LOG4J_SPI = "org.apache.logging.log4j.spi.ExtendedLogger"在Log4j2包log4j-api中
//LOG4J_SLF4J_PROVIDER = 
//     "org.apache.logging.slf4j.SLF4JProvider"在Log4j2桥接器包log4j-to-slf4j中
//SLF4J_SPI = "org.slf4j.spi.LocationAwareLogger" 在slf4j包 slf4j-api 中
//SLF4J_API = "org.slf4j.Logger" 在slf4j包 slf4j-api 中//判断是否导入了 log4j2包 log4j-apiif (isPresent(LOG4J_SPI)) {if (//判断是否导入了 Log4j2 桥接器包log4j-to-slf4jisPresent(LOG4J_SLF4J_PROVIDER) &&//判断是否导入了 slf4j包 slf4j-apiisPresent(SLF4J_SPI)) {// log4j-to-slf4j bridge -> we'll rather go with the SLF4J SPI;// however, we still prefer Log4j over the plain SLF4J API since// the latter does not have location awareness support.logApi = LogApi.SLF4J_LAL;}else {// Use Log4j 2.x directly, including location awareness supportlogApi = LogApi.LOG4J;}}else if (isPresent(SLF4J_SPI)) {// Full SLF4J SPI including location awareness supportlogApi = LogApi.SLF4J_LAL;}else if (isPresent(SLF4J_API)) {// Minimal SLF4J API without location awareness supportlogApi = LogApi.SLF4J;}else {// java.util.logging as defaultlogApi = LogApi.JUL;}}// LOG4J  采用 log4j2的 日志实现// SLF4J_LAL 采用 slf4j  Slf4jLocationAwareLog Full SLF4J SPI 
//including location awareness support// SLF4J 采用 slf4j  Slf4jLog  Minimal SLF4J API without 
//location awareness supportprivate enum LogApi {LOG4J, SLF4J_LAL, SLF4J, JUL}public static Log createLog(String name) {switch (logApi) {case LOG4J:return Log4jAdapter.createLog(name);case SLF4J_LAL:return Slf4jAdapter.createLocationAwareLog(name);case SLF4J:return Slf4jAdapter.createLog(name);default:// Defensively use lazy-initializing adapter class here as well since the// java.logging module is not present by default on JDK 9. We are requiring// its presence if neither Log4j nor SLF4J is available; however, in the// case of Log4j or SLF4J, we are trying to prevent early initialization// of the JavaUtilLog adapter - e.g. by a JVM in debug mode - when eagerly// trying to parse the bytecode for all the cases of this switch clause.return JavaUtilAdapter.createLog(name);}
}

关键字:Spring5 的日志学习

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: