简体   繁体   English

为什么Java spring boot logback不记录异常

[英]Why Java spring boot logback not logging exceptions

I'm new to Spring Boot and I just have setup the logback to write logs to a custom log file. 我是Spring Boot的新手,我只是设置了logback来将日志写入自定义日志文件。 Everything works fine, I have successfully writing custom log messages into the file, however when an exception is raised the error and the stacktrace is not being written to the file. 一切正常,我已成功将自定义日志消息写入文件,但是当引发异常时,错误和堆栈跟踪不会写入文件。

src/main/resources/ logback.xml src / main / resources / logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern>
        </encoder>
    </appender>

    <timestamp key="timestamp" datePattern="yyyy-MM-dd"/>

    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>logs/app-${timestamp}.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- daily rollover -->
            <fileNamePattern>logFile.%d{yyyy-MM-dd}.log</fileNamePattern>
            <!-- retain 30 days logs -->
            <maxHistory>30</maxHistory>
        </rollingPolicy>

        <encoder>
            <pattern>%date [%level] from %logger in %thread - %message%n%xException</pattern>
        </encoder>
    </appender>

    <root level="INFO">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="FILE" />
    </root>
</configuration>

src/main/resources/ application.properties src / main / resources / application.properties

logging.config=classpath:logback.xml

The main class: 主要课程:

@SpringBootApplication
public class DemoApplication {

    private static final Logger logger = Logger.getLogger("DEMO");

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

        logger.log(Level.INFO, "main called");

        throw new IllegalArgumentException("This is a test");
    }
}

And the stacktrace which is not in the log file: 并且堆栈跟踪不在日志文件中:

Exception in thread "main" java.lang.IllegalArgumentException: This is a test
00:06:54.179 [main] ERROR DEMO - This is an error
    at com.demo.DemoApplication.main(DemoApplication.java:18)

PS: I have tried to change the root level logging: <root level="ERROR"> but the issue still persist, I can't see the exception in the log file. PS:我曾尝试更改根级别日志记录: <root level="ERROR">但问题仍然存在,我无法在日志文件中看到异常。

1.you must use slf4j + logback , it seem you are using java util logging 2. try catch exception and log the error, otherwise the logback can't catch the exception 1.你必须使用slf4j + logback ,看来你正在使用java util logging 2.尝试捕获异常并记录错误,否则logback无法捕获异常

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class DemoApplication {

    private static final Logger logger = LoggerFactory.getLogger("DEMO");

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

        logger.info("main called");
        try {
            throw new IllegalArgumentException("This is a test");
        } catch(Exception e) {
            logger.error("", e);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM