我有一个弹簧靴休息服务。当我向服务发送请求时,我想记录它是否是成功的请求。如果其余服务成功,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 部分
慕神8447489
慕娘9325324
相关分类