springboot和apache logger日志框架详解(内含log4j2-spring.xml配置)

https://www.cnblogs.com/pjfmeng/p/11277124.html (包含log4j2源码和解析)
https://www.cnblogs.com/jice/p/12310852.html (log4j2真正的jar包:-core:2.x和-api:2.x)
https://www.cnblogs.com/xuxinstyle/p/10960026.html (logger格式讲解和测试代码)
https://cloud.tencent.com/developer/article/1435719
https://blog.csdn.net/flysun3344/article/details/80555746
https://janus.blog.csdn.net/article/details/80632121 (内含springboot包依赖和包含关系图,十分详细,内含log4j2-spring.xml配置)
https://blog.csdn.net/qq_27706119/article/details/104977666 内含log4jyyy.xml配置的详细解析
https://blog.csdn.net/chenwq726/article/details/85066931 (slf4j、log4j2、log4j-slf4j-impl关系)
https://www.cnblogs.com/xing901022/p/4149524.html (slf4j、log4j2、log4j-slf4j-impl关系,pom.xml引用log4j-slf4j-impl-2.x版本即实现了对接log4j2,log4j2真正的jar包:-core:2.x和-api:2.x)
https://blog.csdn.net/qq_44002865/article/details/106105533 常用日志门面和日志实现 (异步日志配置

)
在pom.xml做配置即可使用

org.springframework.boot
spring-boot-starter-logging

日志框架:slf4j
日志实现:log4j2
桥接包:log4j-slf4j-impl

桥接包log4j-slf4j-impl起到适配的作用,因为市面上的日志实现互不兼容,日志框架slf4j要想适用于日志实现log4j2,就需要使用桥接包

slf4j使用LoggerFactory创建Logger进行日志打印,底层实际上调用了log4j-slf4j-impl的StaticLoggerBinder类创建一个Log4jLoggerFactory,然后再由这个Log4jLoggerFactory创建一个Log4j2的Logger对象,这个Logger封装在log4j-slf4j-impl中的Log4jLogger里面,最后将Log4jLogger返回给slf4j,每次slf4j进行日志打印,实际上是log4j-slf4j-impl中的Log4jLogger调用log4j2进行日志打印
如果没有 log4j-slf4j-impl桥接包,slf4j将创建一个对象,里面都是空方法,所以不会打印出日志

log4j2配置文件的优先级(调用log4j配置文件的优先级)
Log4j will inspect the log4j.configurationFile system property and, if set, will attempt to load the configuration using the ConfigurationFactory that matches the file extension.

If no system property is set the properties ConfigurationFactory will look for log4j2-test.properties in the classpath.

If no such file is found the YAML ConfigurationFactory will look for log4j2-test.yaml or log4j2-test.yml in the classpath.

If no such file is found the JSON ConfigurationFactory will look for log4j2-test.json or log4j2-test.jsn in the classpath.

If no such file is found the XML ConfigurationFactory will look for log4j2-test.xml in the classpath.

If a test file cannot be located the properties ConfigurationFactory will look for log4j2.properties on the classpath.

If a properties file cannot be located the YAML ConfigurationFactory will look for log4j2.yaml or log4j2.yml on the classpath.

If a YAML file cannot be located the JSON ConfigurationFactory will look for log4j2.json or log4j2.jsn on the classpath.

If a JSON file cannot be located the XML ConfigurationFactory will try to locate log4j2.xml on the classpath.

If no configuration file could be located the DefaultConfiguration will be used. This will cause logging output to go to the console.

Logger
Loggers组件被分为七个级别:

级别 对应int值
OFF 0,这是最高等级,为了关闭日志记录
FATAL 100,指定非常严重的错误事件
ERROR 200
WARN 300
INFO 400
DEBUG 500
TRACE 600
ALL Integer.MAX_VALUE,各级包括自定义级别
各个级别的顺序是这样那个的:

OFF < DEBUG < INFO < WARN < ERROR < FATAL

可以简单地理解为级别越大越重要。
Log4j有一个规则:只输出级别不低于设定级别的日志信息,假设Loggers级别设定为INFO,则INFO、WARN、ERROR、FATAL和ALL级别的日志信息都会输出,而级别比INFO低的DEBUG则不会输出。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值