踩了点小坑,springboot2.x默认是使用的是logback作为日志框架,想用log4j2的话,需要排除logback依赖,引入log4j2的依赖。mvn看下logback是哪里依赖的,我这里是spring-boot-starter-web,排除掉这个默认的spring-boot-starter-logging,然后引入spring-boot-starter-log4j2,安安静静配置log4j2的yml文件。在代码里加入日志,看下效果。
启动application的时候,看到了一行红色错误提示:
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console. Set system property 'log4j2.debug' to show Log4j2 internal initialization logging.
我踏马隐约看到一个属性key,log4j2.debug,提示给这个键设值就可以看到log4j2内部初始化日志了,这还是个system property。
直觉告诉我,log4j2初始化出错了。我就想着赶紧给这个log4j2.debug设个值,然后看下有关log4j2自己的日志。一个简单粗暴的方法飞快闪过电脑屏幕,
System.setProperty("log4j2.debug", "true");
重新启动,事实证明,哥的直觉是准确的,我看到了一些不该看到的东西:
TRACE StatusLogger Trying to find [log4j2.yml] using context class loader sun.misc.Launcher$AppClassLoader@73d16e93.
WARN StatusLogger Found configuration file log4j2.yml for inactive ConfigurationFactory org.apache.logging.log4j.core.config.yaml.YamlConfigurationFactory。
log4j2.yml inactive YamlConfigurationFactory,三个关键字,貌似yml格式的配置文件没有被激活。打开源文件看下什么情况,请允许我贴一张图,不想解释了:
少了点东西,jacksonformat,引入如下依赖:
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
</dependency>
再次启动,日志打印正常了,心中满满都是感谢,感谢log4j2错误提示如此到位,可以让我定位问题,并找到解决之道