Hello, everyone, I’m the black brother downstairs ~

Recently, I was sent to Beijing for a business trip by my company. I thought it was an easy job

But did not expect the first day is nine thirty off work, careless ~

Ok, back to business, today I will talk about a recent debug project that found a Logback log level setting does not take effect.

The problem background

The thing is, our project is a SpringBoot project, where the logging framework uses LogBack, the configuration file is as follows:

<configuration scan="true" scanPeriod=" 5 seconds" debug="true">

    <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%date [%thread] %-5level %logger{80} - %msg%n</pattern>
        </encoder>
    </appender>

    <logger name="org.springframework" level="DEBUG"/>

    <root level="debug">
        <appender-ref ref="stdout"/>
    </root>

</configuration>
Copy the code

To make it easier to see the SQL being executed by the project, I set the log level to DEBUG.

The execution result of the running project is quite surprising. Only INFO logs are output, and no DEBUG logs are output.

At first I thought there was something wrong with the Logback configuration file that caused the problem. I found a few examples online and compared them, and there is nothing wrong with this type of configuration file.

A series of in-depth investigations led to the discovery of the cause of the problem.

Question why

Because Logback configuration debug=true, internal Logback logs are printed when the project is started. The logs are as follows:

As you can see from this log, Logback Root has been set to DEBUG.

Then why does DEBUG fail after the project starts?

Don’t worry, keep reading.

When the Spring container is started, Spring emits a list of ApplicationEvents, which are then listened to by various registered ApplicationListeners.

As a result of this project is a SpringBoot project, there is a LoggingApplicationListener will monitor ApplicationEnvironmentPreparedEvent, code is as follows:

The logic is simple. Obtain the log level of the system configuration as follows:

Root = info; Logback = info;

These log level configurations are actually from the SpringBoot configuration file application.properties

conclusion

Finally, to summarize, SpringBoot projects that use a separate LogBack configuration file, Properties configuration logging.level.root will override the root configuration in the Logback configuration file:

<root level="debug">
    <appender-ref ref="stdout"/>
</root>
Copy the code

The problem seems so simple, and the process can be mind-boggling.