解决springboot 2.x集成log4j2调试日志无法关闭的问题

时间:2022-07-03 07:06:14

springboot2.x集成log4j2时,始终无法关闭log4j2自身的日志输出

已经做了如下配置:

在log4j2.xml的配置文件中,配置configuration的status属性为OFF;

确认系统所有地方无配置log4j2.debug;

如上配置都无法解决问题,只能从源码着手一探究竟。

从log4j2-api包中,找到StatusLogger,其设置日志输出level的代码如下:

private StatusLogger(final String name,final MessageFactory messageFactory) {
        super(name, messageFactory);
        final String dateFormat = PROPS.getStringProperty(STATUS_DATE_FORMAT, Strings.EMPTY);
        final boolean showDateTime = !Strings.isEmpty(dateFormat);
        this.logger =new SimpleLogger("StatusLogger", Level.ERROR,false,true, showDateTime,false,
                dateFormat, messageFactory, PROPS, System.err);
        this.listenersLevel = Level.toLevel(DEFAULT_STATUS_LEVEL, Level.WARN).intLevel();
 
        // LOG4J2-1813 if system property "log4j2.debug" is defined, print all status logging
        if (isDebugPropertyEnabled()) {
            logger.setLevel(Level.TRACE);
        }
    }

从上述代码可以看出,level的级别默认是设置为error,仅当有设置log4j2.debug时,才会输出trace日志。

那log4j2.debug属性在哪设置的呢?带着这个问题,寻找到了SystemPropertiesPropertySource。这个类在装载属性到Environment前有做自定义处理:

private static final String PREFIX ="log4j2.";
 
@Override
public CharSequence getNormalForm(final Iterable<?extends CharSequence> tokens) {
    return PREFIX + Util.joinAsCamelCase(tokens);
}

如上述代码所示,该操作会解释所有系统属性,然后按解析后的token自行加上log4j2.的前缀。在这里导致了日志系统认为需要debug,进而输出trace日志的问题。

那系统属性上的debug哪来的呢?

首先,本地进程是以run模式启动的,环境变量也没有自行设置debug参数,为何会有jvm启动参数中会有 “-Ddebug” 的存在?

查看运行进程的Configurations配置如下所示:

解决springboot 2.x集成log4j2调试日志无法关闭的问题

springboot有一个自定义配置,默认是勾上了enable debug output。把这个去掉,再次运行发现-Ddebug没有了,日志也正常了。

不过即便如此,log4j2是不是有点智障,这么拼接系统变量的搞法很容易混淆

springboot整合log4j2遇到的一个坑

项目中使用springboot,需要用log4j2做日志框架

问题

项目启动报错:Could not initialize Log4J2 logging from classpath:log4j2-dev.yml

解决springboot 2.x集成log4j2调试日志无法关闭的问题

是一个无法初始化Log4J2配置的问题,项目中采用的yml的配置文件。

前置操作

首先引入依赖:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j2</artifactId>
    </dependency>

去掉默认的logback配置:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <exclusions><!-- 去掉默认配置 -->
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

添加配置文件:

解决springboot 2.x集成log4j2调试日志无法关闭的问题

配置logging.config

解决springboot 2.x集成log4j2调试日志无法关闭的问题

以上是整合操作的必要配置,配置完成启动报错。

问题排查

根据异常信息描述,找到源码中初始化的代码:Log4J2LoggingSystem.loadConfiguration

解决springboot 2.x集成log4j2调试日志无法关闭的问题

继续跟进:

解决springboot 2.x集成log4j2调试日志无法关闭的问题

发现是一个抽象方法,idea中使用ctrl+alt+B查找实现类:

解决springboot 2.x集成log4j2调试日志无法关闭的问题

因为使用的是yml,所以实现类应该是YamlConfig这个,找到具体实现:

解决springboot 2.x集成log4j2调试日志无法关闭的问题

通过debug发现isActive是false,所以返回null。在此类中找到isActive的含义:

解决springboot 2.x集成log4j2调试日志无法关闭的问题

这是关键的逻辑,原来回去检查是否存在上面几个依赖的类,调试返现没有YAMLFactory这个类,可以看出这个类是jackson包中的,看来是少依赖了包。

解决

引入jackson-dataformat-yaml依赖:

    <dependency>
        <groupId>com.fasterxml.jackson.dataformat</groupId>
        <artifactId>jackson-dataformat-yaml</artifactId>
    </dependency>

启动正常,问题解决。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://www.cnblogs.com/asfeixue/p/13112169.html