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. However, when an SQL exception is thrown, it does not log the logger.error portion to console. 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:
<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
