为什么logback不使用Spring Boot记录某些行? [英] Why logback is not logging some lines with Spring Boot?

查看:152
本文介绍了为什么logback不使用Spring Boot记录某些行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to log some lines to a log files using logback. The file is correctly created and my console output is actually written in the log file.

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

Then, I inserted some logger.debug() instructions in my code, which I don't find in my log. Why?

我正在使用Spring Boot:

I am using Spring Boot:

@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并创建了此文件.

application.properties includes logging.file=logs/mylog.log and this file is created.

登录配置非常简单,应该正确并正确放置(如果我更改此文件,例如,通过引入日志文件名称模式,它就可以工作):

Logback configuration is very easy and it should be correct and correctly placed (if I change this file, e.g. by introducing a log file name pattern, it works):

<?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吗?如何记录这些行?

Why I don't see my log instructions in my log file? Is it because I am building an InitializingBean? How can I log these lines?

推荐答案

这是由于base.xml文件的根日志记录设置为INFO级别.请尝试此操作以获取调试日志:-

Its due to base.xml file which has root logging set at INFO level. Pls try this to get your debug logs :-

<?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>

这篇关于为什么logback不使用Spring Boot记录某些行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆