【问题标题】:Why Java spring boot logback not logging exceptions为什么Java spring boot logback不记录异常
【发布时间】:2019-02-27 10:25:01
【问题描述】:

我是 Spring Boot 的新手,我刚刚设置了 logback 以将日志写入自定义日志文件。一切正常,我已经成功地将自定义日志消息写入文件,但是当引发异常时,错误并且堆栈跟踪没有被写入文件。

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

logging.config=classpath:logback.xml

主类:

@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");
    }
}

还有不在日志文件中的stacktrace:

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)

P.S:我已尝试更改根级别日志记录:&lt;root level="ERROR"&gt; 但问题仍然存在,我在日志文件中看不到异常。

【问题讨论】:

  • 这是来自哪里的错误消息?我可以看到堆栈跟踪,它说它是在第 18 行抛出的,那么问题是什么?
  • 它在我的控制台中,但不在日志文件中
  • 使用您在控制台附加程序配置中获得的相同模式。
  • 与 micronaut 有完全相同的问题。 throw new Exception() 既没有出现在控制台中,也没有出现在文件中。

标签: java spring-boot logging


【解决方案1】:

1.你必须使用slf4j + logback,看来你使用的是java util logging 2.尝试catch异常并记录错误,否则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);
        }
    }
}

【讨论】:

  • 这并不能解决问题。
猜你喜欢
  • 2019-07-06
  • 1970-01-01
  • 2015-07-10
  • 2017-01-21
  • 1970-01-01
  • 2022-07-15
  • 2021-08-27
  • 2021-03-15
  • 1970-01-01
相关资源
最近更新 更多