【问题标题】:Java Spring Boot not logging exceptionsJava Spring Boot不记录异常
【发布时间】:2019-07-06 06:16:50
【问题描述】:

我有一个弹簧靴休息服务。当我向服务发送请求时,我想记录它是否是成功的请求。如果其余服务成功,logger.info(任何记录器甚至 logger.error 都会被记录)将被记录到控制台。但是,当抛出 SQL 异常时,它不会将 logger.error 部分记录到控制台。我如何得到它,所以如果抛出异常,它也会将该部分记录到控制台?

这里是测试方法:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory   

public String testMethod(){
    try{

        LOGGER.info("running test method");

        Class.forName("oracle.jdbc.driver.OracleDriver");

        Connection test = DriverManager.getConnection(
                datasourceUrl,datasourceUsername,datasourcePassword);
        //Select data
        Statement stmt = test.createStatement();

        InputStream input = getClass().getResourceAsStream("/testMethodSQL.sql");
        String insertTableSQL = IOUtils.toString(input);

         PreparedStatement preparedStatement = prod.prepareStatement(insertTableSQL);

        ResultSet rs = preparedStatement.executeQuery();

        }

        test.close();

        LOGGER.info("test Method successfully ran");

        return "Done";
    }
    catch (Exception e) {
        e.printStackTrace();
        LOGGER.error("Error found: {}", e);
        return "An error occured: " +  e;
    }

}

这是我的 logback.xml 文件:

<configuration>
<appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
    <encoder class="net.logstash.logback.encoder.LogstashEncoder">
    </encoder>
</appender>

<root level="info">
    <appender-ref ref="Console" />
</root>

</configuration>

例如,当抛出异常时,结果会打印出来:

{"@timestamp":"2019-02-12T10:07:05.989-05:00","@version":1,"message":"running test method","logger_name":"com.test","thread_name":"http-nio-8080-exec-2","level":"INFO","level_value":20000,"RequestId":"6D71709D5A1A4C83876B68A661D696E3"}

java.sql.SQLIntegrityConstraintViolationException: ORA-00001: unique constraint (TEST_TABLE.SYS_C0013283) violated

但是,它不会记录 Logger.error 部分

【问题讨论】:

    标签: java spring-boot logging


    【解决方案1】:

    可能是您的 IDE 抑制了日志条目。我最近在 IntelliJ 中遇到了这个问题。检查是否有扩展日志条目的选项

    在发现扩展日志的选项之前,我最终得到了两个最小的示例项目。一个项目使用了 spring + logback,另一个项目只使用了 logback。奇怪的是,仅使用 logback 的示例 IntelliJ 没有执行任何抑制。

    【讨论】:

      【解决方案2】:

      要启用日志记录,请在资源文件夹的根目录中创建一个application.properties 文件,并且您需要在application.properties 中添加日志记录级别

      logging.level.org.springframework.web=ERROR
      logging.level.com.mkyong=DEBUG
      

      或者只是将它们添加到您的 logback.xml 中

          <logger name="com.mkyong" level="debug"
              additivity="false">
              <appender-ref ref="FILE-AUDIT" />
          </logger>
      
          <root level="error">
              <appender-ref ref="FILE-AUDIT" />
          </root>
      

      【讨论】:

      • 我已经尝试在我的 logback.xml 上添加调试级别,并且当仍然抛出异常时它不会记录“logger.error”部分
      • 你在代码中使用LOGGER.error("Error found: {}", e);,对我来说root level应该是"error"而不是"info"
      • 如果像我一样,如果您的@ExceptionHandler 中有错误,则错误级别还不够,要对其进行调试:logging.level.org.springframework.web.servlet.mvc=DEBUG
      猜你喜欢
      • 2019-02-27
      • 2018-01-14
      • 1970-01-01
      • 2016-09-29
      • 2022-11-22
      • 1970-01-01
      • 2014-08-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多