当前位置: 首页 > news >正文

Spring5 的日志学习

我们在使用 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);}
}

http://www.lryc.cn/news/414908.html

相关文章:

  • python爬虫实践
  • 【前端面试】七、算法-数组展平
  • Laravel php框架与Yii php 框架的优缺点
  • 使用 addRouteMiddleware 动态添加中间
  • Zookeeper未授权访问漏洞
  • 【JavaEE】定时器
  • 2024带你轻松玩转Parallels Desktop19虚拟机!让你在Mac电脑上运行Windows系统
  • 【算法】递归实现二分查找(优化)以及非递归实现二分查找
  • CDN 是什么?
  • 索引:SpringCloudAlibaba分布式组件全部框架笔记
  • 2024第五届华数杯数学建模竞赛C题思路+代码
  • FFmpeg源码:av_reduce函数分析
  • nginx: [error] open() “/run/nginx.pid“ failed (2: No such file or directory)
  • <数据集>BDD100K人车识别数据集<目标检测>
  • 利用SSE打造极简web聊天室
  • 代码随想录第二十天|动态规划(4)
  • TreeSize:免费的磁盘清理与管理神器,解决C盘爆满的燃眉之急
  • 如何建立自己的技术知识体系
  • JS优化了4个自定义倒计时
  • 模型实战(25)之 基于LoFTR深度学习匹配算法实现图像拼接
  • 计算机毕业设计Python+Spark知识图谱高考志愿推荐系统 高考数据分析 高考可视化 高考大数据 大数据毕业设计
  • 【python】文件
  • 《Attention Is All You Need》核心观点及概念
  • 【中项】系统集成项目管理工程师-第9章 项目管理概论-9.9价值交付系统
  • JS+H5美观的带搜索的博客文章列表(可搜索多个参数)
  • 牛客周赛 Round 54 (c++题解)
  • htsjdk库Genotype及相关类介绍
  • C++ 最短路(spfa) 洛谷
  • MySQL的数据类型
  • xss漏洞(四,xss常见类型)