1

Hello, everyone, I am the little black brother downstairs~

I was recently sent to Beijing on a business trip by the company. I thought it was an easy errand. I visited Beijing for a week~

But I didn’t expect to get off work at half past nine on the first day.

Okay, back to the topic, today I'm going to talk about a problem that the Logback log level setting does not take effect when I recently debugged the project.

Problem background

The thing is like this, our project is a SpringBoot project, in which the log 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>

In order to facilitate viewing the SQL executed by the project, here I adjusted the log level to DEBUG .

Operation of the project, the results of the comparison Surprisingly, only the log output INFO log, and no output DEBUG log.

At first, I thought that there was a problem with the writing of the Logback configuration file, which caused this problem. I found a few examples on the Internet and compared them. There is no problem with this type of configuration file.

So a series of in-depth investigations (to make people bald) finally found the cause of the problem.

problem causes

Due to the configuration of debug=true , when the project is started, the internal log information of Logback will be printed out, the log is as follows:

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

Then why does DEBUG fail after the project is started?

Don't worry, then look down.

When the Spring container is started, a series of ApplicationEvent will be sent out inside Spring, and these will be monitored ApplicationListener

Since this project is a SpringBoot project, there is a LoggingApplicationListener will listen to ApplicationEnvironmentPreparedEvent . The code is as follows:

The logic here is relatively simple. To obtain the log level of the system configuration, the levels is as follows:

You can see that the value corresponding to root here is info , here will continue to call the method of Logback to set the log level, the log output is as follows:

The above log level configuration comes from the configuration file application.properties

to sum up

Finally, to summarize, if a separate LogBack configuration file is used in the SpringBoot project, the SpringBoot configuration file application.properties configuration logging.level.root will overwrite the configuration of root in the Logback configuration file:

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

The problem seems so simple, the process of investigation is really bald.


楼下小黑哥
359 声望39 粉丝

非专业程序员