本文乃博主呕心沥血之作,一文搞清楚java所有日志框架。阅读前请先收藏。
核心日志框架,就是实际干活的日志框架。总体而言,市面上的使用日志框架体系主要有
- jul(java.util.logging) jdk1.4加入,为了对抗log4j,效率灵活性较差使用较少
- log4j 最广泛应用的日志框架,成为事实上的标准
- logback 基于slf4j-api接口实现,性能高于log4j
- log4j2 重写了log4j,性能高于log4j,logback
核心日志框架能单独使用,但多框架集成使用时使用会有冲突。所以出现了门面日志框架。
门面日志框架特征有:
- 提供统一日志使用接口,核心日志框架去实现门面日志框架的接口。
- 应用不使用具体的核心日志框架,只使用门面日志框架。不依赖核心日志框架,只依赖门面日志框架。
- 这样就算底层换核心框架依赖,不影响现有日志的使用。
目前主流的门面框架主要有JCL和SLF4J:
- JCL(commings-log) Apache提供的comming-log
- SLF4J(simple log facade for java) Log4j、Logback、Log4j2作者提供
各核心日志框架单独使用的依赖,demo里的version不限制。
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>1.2.17version>
dependency>
<dependency>
<groupId>org.apache.logging.log4jgroupId>
<artifactId>log4j-apiartifactId>
<version>2.9.1version>
dependency>
<dependency>
<groupId>org.apache.logging.log4jgroupId>
<artifactId>log4j-coreartifactId>
<version>2.9.1version>
dependency>
<dependency>
<groupId>ch.qos.logbackgroupId>
<artifactId>logback-coreartifactId>
<version>1.2.3version>
dependency>
<dependency>
<groupId>ch.qos.logbackgroupId>
<artifactId>logback-classicartifactId>
<version>1.2.3version>
dependency>
jdk自带API,无依赖包
<dependency>
<groupId>commons-logginggroupId>
<artifactId>commons-loggingartifactId>
<version>1.1.1version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
<version>1.7.25version>
dependency>
总结如下:
- JCL集成其他日志框架,只有log4j/jul没有中间包
- JCL同时集成其他核心日志框架,使用JCL打印日志优先级: log4j2>log4j>jul
- JCL同时和jul/log4j/log4j2集成,jcl没有全局整合能力各日志全部生效
- JCL同时和jul/log4j/log4j2集成,此时使用JCL打印日志生效的是log4j2
- slf4j转向JCL前提是没有slf4j的实现框架依赖,否则slf4j实现优先级更高
默认jcl就是集成jul的
<dependency>
<groupId>commons-logginggroupId>
<artifactId>commons-loggingartifactId>
<version>1.1.3version>
dependency>
<dependency>
<groupId>commons-logginggroupId>
<artifactId>commons-loggingartifactId>
<version>1.1.3version>
dependency>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>1.2.17version>
dependency>
<dependency>
<groupId>commons-logginggroupId>
<artifactId>commons-loggingartifactId>
<version>1.2version>
dependency>
<dependency>
<groupId>org.apache.logging.log4jgroupId>
<artifactId>log4j-apiartifactId>
<version>2.9.1version>
dependency>
<dependency>
<groupId>org.apache.logging.log4jgroupId>
<artifactId>log4j-coreartifactId>
<version>2.9.1version>
dependency>
<dependency>
<groupId>org.apache.logging.log4jgroupId>
<artifactId>log4j-jclartifactId>
<version>2.9.1version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
<version>1.7.25version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-jclartifactId>
<version>1.7.25version>
dependency>
总结如下:
- SLF4J转向其他日志框架都需要对应适配包
- 其他日志框架经过JCL转向SLF4J时,一般可以省略JCL通过匹配包直接转向SLF4J不再展开
- SLF4J和具体日志框架的双向适配包不能同时存在(需排除冲突),否则会循环依赖栈溢出.包括:
slf4j-jdk14和jul-to-slf4j(运行时直接栈溢出)
slf4j-log4j12和log4j-over-slf4j(启动会检测报错)
log4j-slf4j-impl和log4j-to-slf4j(运行时直接栈溢出)
slf4j-jcl和jcl-over-slf4j(启动会检测报错)
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
<version>1.7.25version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-jdk14artifactId>
<version>1.7.25version>
dependency>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>1.2.17version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
<version>1.7.25version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
<version>1.7.25version>
dependency>
<dependency>
<groupId>org.apache.logging.log4jgroupId>
<artifactId>log4j-apiartifactId>
<version>2.9.1version>
dependency>
<dependency>
<groupId>org.apache.logging.log4jgroupId>
<artifactId>log4j-coreartifactId>
<version>2.9.1version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
<version>1.7.25version>
dependency>
<dependency>
<groupId>org.apache.logging.log4jgroupId>
<artifactId>log4j-slf4j-implartifactId>
<version>2.9.1version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
<version>1.7.25version>
dependency>
<dependency>
<groupId>ch.qos.logbackgroupId>
<artifactId>logback-coreartifactId>
<version>1.2.3version>
dependency>
<dependency>
<groupId>ch.qos.logbackgroupId>
<artifactId>logback-classicartifactId>
<version>1.2.3version>
dependency>
<dependency>
<groupId>commons-logginggroupId>
<artifactId>commons-loggingartifactId>
<version>1.1.1version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
<version>1.7.25version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-jclartifactId>
<version>1.7.25version>
dependency>
slf4j获取具体框架的流程如下,可得知slf4j最终只能转向单个日志框架。
LoggerFactory.getLogger触发初始化
-> 根据classLoader查找org/slf4j/impl/StaticLoggerBinder.class,check不能有多个
-> 触发org.slf4j.impl.StaticLoggerBinder(不同集成框架路径相同实现不同)的getBean初始化
-> 报告StaticLoggerBinder的实际采用
-> slf4j版本检验
-> 根据StaticLoggerBinder获取org.slf4j.ILoggerFactory
-> 根据ILoggerFactory获取到org.slf4j.Logger
两个或以上的日志框架使用时,需要整合。达到以下目标:
- 全部日志API生效,能正常输出日志
- 排除冲突,避免循环转换栈溢出
- 排除冲突,避免slf4j转向实现框架
根据最终转向的日志框架分类:
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>1.2.17version>
dependency>
<dependency>
<groupId>org.apache.logging.log4jgroupId>
<artifactId>log4j-apiartifactId>
<version>2.9.1version>
dependency>
<dependency>
<groupId>org.apache.logging.log4jgroupId>
<artifactId>log4j-coreartifactId>
<version>2.9.1version>
dependency>
<dependency>
<groupId>ch.qos.logbackgroupId>
<artifactId>logback-coreartifactId>
<version>1.2.3version>
dependency>
<dependency>
<groupId>ch.qos.logbackgroupId>
<artifactId>logback-classicartifactId>
<version>1.2.3version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
<version>1.7.25version>
dependency>
<dependency>
<groupId>commons-logginggroupId>
<artifactId>commons-loggingartifactId>
<version>1.1.1version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-jdk14artifactId>
<version>1.7.25version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>jul-to-slf4jartifactId>
<version>1.7.25version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
<version>1.7.25version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>log4j-over-slf4jartifactId>
<version>1.7.25version>
dependency>
<dependency>
<groupId>org.apache.logging.log4jgroupId>
<artifactId>log4j-slf4j-implartifactId>
<version>2.9.1version>
dependency>
<dependency>
<groupId>org.apache.logging.log4jgroupId>
<artifactId>log4j-to-slf4jartifactId>
<version>2.9.1version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-jclartifactId>
<version>1.7.25version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>jcl-over-slf4jartifactId>
<version>1.7.25version>
dependency>
<dependency>
<groupId>org.apache.logging.log4jgroupId>
<artifactId>log4j-jclartifactId>
<version>2.9.1version>
dependency>
slf4j-jdk14和jul-to-slf4j(运行时直接栈溢出)
slf4j-log4j12和log4j-over-slf4j(启动会检测报错)
log4j-slf4j-impl和log4j-to-slf4j(运行时直接栈溢出)
slf4j-jcl和jcl-over-slf4j(启动会检测报错)
使用slf4j整合其他框架时,只能转向单个日志框架,即class路径只能有一个org.slf4j.impl.StaticLoggerBinder
因此以下包不能同时使用,只能出现一个:
slf4j-jdk14
slf4j-log4j12
log4j-slf4j-impl
logback-classic
slf4j-jcl
比如:
log4j.jar 低版本的和高版本冲突:目前测试下来:log4j1.2.6和1.2.17 两个jar同时引入导致日志不能打印