[英]Java Spring Boot not logging exceptions
I have a spring boot rest service. 我有一个弹簧靴休息服务。 When I send a request to the service, I would like to log whether or not it was a successful request or not. 当我向服务发送请求时,我想记录该请求是否成功。 If the rest service is successfully, logger.info (any logger will be logged even logger.error) will be logged to the console. 如果其余服务成功,则logger.info(即使logger.error也会记录任何记录器)将记录到控制台。 However, when an SQL exception is thrown, it does not log the logger.error portion to console. 但是,当引发SQL异常时,它不会将logger.error部分记录到控制台。 How do I get it so if an exception is thrown, it would also log that portion to the console? 我如何获得它,如果引发异常,它也会将该部分记录到控制台?
Here is the test method: 这是测试方法:
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;
}
}
Here is my logback.xml file: 这是我的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>
For example, when an exception is thrown the results would print out: 例如,当引发异常时,结果将打印出来:
{"@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
However, it does not log the Logger.error portion 但是,它不会记录Logger.error部分
It could be your IDE suppressing the log entry. 这可能是您的IDE禁止记录日志。 I encountered this recently with IntelliJ. 我最近在IntelliJ中遇到了这个问题。 Check if there is an option to expand your log entries 检查是否有扩展日志条目的选项
I ended up with two minimal example projects before spotting the option to expand the logs. 在发现扩展日志的选项之前,我最后给出了两个最小的示例项目。 One project used spring + logback and the other just used logback. 一个项目使用spring + logback,另一个项目仅使用logback。 Weirdly for the example with logback only IntelliJ didn't perform any supression. 奇怪的是,只有Logical Logback的示例IntelliJ没有执行任何抑制。
To enable logging, create a application.properties
file in the root of the resources folder, And you need add logging level in your application.properties
要启用日志记录,请在resources文件夹的根目录中创建一个application.properties
文件,并且需要在application.properties
添加日志记录级别
logging.level.org.springframework.web=ERROR
logging.level.com.mkyong=DEBUG
Or just add those in your logback.xml 或者只是将它们添加到您的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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.