为什么 logback 不使用 Spring Boot 记录一些行?

Why logback is not logging some lines with Spring Boot?

我正在尝试使用 logback 将一些行记录到日志文件中。文件创建正确,我的控制台输出实际上写在日志文件中。

然后,我在我的代码中插入了一些 logger.debug() 指令,这些指令在我的日志中找不到。为什么?

我正在使用 Spring 引导:

@SpringBootApplication
public class MyApplication {

    private static final Logger logger =
            LoggerFactory.getLogger(MyApplication.class);

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }

    @Bean
    InitializingBean myInitializer(final IdentityService identityService) {
        return new InitializingBean() {
            public void afterPropertiesSet() throws Exception {
                logger.debug("Preparing things...");
                if (someCondition) {
                    doSomething();
                    logger.debug("Done something.");
                } else {
                    doSomethingElse();
                    logger.debug("Done something else.");
                }

            }
        };
    }
}

application.properties 包含 logging.file=logs/mylog.log 并创建此文件。

Logback 配置非常简单,它应该正确且放置正确(如果我更改此文件,例如通过引入日志文件名模式,它会起作用):

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/base.xml"/>
    <logger name="org.springframework.web" level="DEBUG"/>
</configuration>

为什么我在我的日志文件中看不到我的日志说明?是因为我正在构建一个 InitializingBean 吗?我如何记录这些行?

这是由于 base.xml 文件将根日志记录设置为 INFO 级别。请试试这个来获取你的调试日志:-

<?xml version="1.0" encoding="UTF-8"?>    
<included>
    <include resource="org/springframework/boot/logging/logback/defaults.xml" />
    <property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}spring.log}"/>
    <include resource="org/springframework/boot/logging/logback/console-appender.xml" />
    <include resource="org/springframework/boot/logging/logback/file-appender.xml" />
    <logger name="org.springframework.web" level="DEBUG"/>
    <root level="DEBUG">
        <appender-ref ref="CONSOLE" />
        <appender-ref ref="FILE" />
    </root>
</included>