Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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

like image 271
John Avatar asked Dec 23 '22 01:12

John


2 Answers

It could be your IDE suppressing the log entry. I encountered this recently with IntelliJ. Check if there is an option to expand your log entries

IDE example

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. Weirdly for the example with logback only IntelliJ didn't perform any supression.

like image 163
spjthe Avatar answered Jan 12 '23 20:01

spjthe


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

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

Or just add those in your 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>
like image 29
Yiao SUN Avatar answered Jan 12 '23 19:01

Yiao SUN