Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot JDBC Template SQL Log

I am trying log SQL queries with params for Spring Boot JDBC but it is not printing the details in log.I am using Spring Boot 1.5.8 version.Please help me to solve this.

application.properties:

spring.datasource.url=url
spring.datasource.username=user
spring.datasource.password=password
spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver

logging.level.org.springframework.jdbc.core.JdbcTemplate=debug

spring.datasource.type = com.zaxxer.hikari.HikariDataSource
spring.datasource.hikari.connection-timeout=60000
spring.datasource.hikari.maximum-pool-size=2

Repository:

@Repository
public class DataRepository {
    private static Logger log = LoggerFactory.getLogger(DataRepository.class);

    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;

    public Data findDataObjet() throws Exception {

        Map<String, Object> parameters = new HashMap<>();
        parameters.put("id1", "mike");
        parameters.put("id2", new Long(1));

        String sqlString = "select * from table1 where id1 = ":id" and id2 = :id2";
        log.info("Query:" + sqlString);//this log is printing

        Data extObj = jdbcTemplate.query(sqlString, parameters, (rs) -> {
            if (rs != null && rs.next()) {
                Data innerObj = new Data();
                innerObj.setName(rs.getString("name"));             
                return innerObj;
            } else {
                log.info("No records found:"+rs);
                return null;
            }
        });

        return extObj;

    }
}

logback-spring.xml:

<appender name="dailyRollingFileAppender"
    class="ch.qos.logback.core.rolling.RollingFileAppender">
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <FileNamePattern>${logsPath}DATA%d{MMddyyyy}.log
        </FileNamePattern>
        <maxHistory>4</maxHistory>
    </rollingPolicy>

    <encoder>
        <Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level
            %logger{35}-%msg %n</Pattern>
    </encoder>
</appender>
<root level="INFO">
    <appender-ref ref="dailyRollingFileAppender" />
</root>

like image 326
sunleo Avatar asked Mar 02 '18 11:03

sunleo


People also ask

Can we use JdbcTemplate in spring boot?

Spring Boot supports H2 (an in-memory relational database engine) and automatically creates a connection. Because we use spring-jdbc , Spring Boot automatically creates a JdbcTemplate . The @Autowired JdbcTemplate field automatically loads it and makes it available.

Does JdbcTemplate use prepared statements?

The JdbcTemplate will create the PreparedStatement and with the callback only being responsible for setting parameter values. This interface contains one method namely, setValues(PreparedStatement ps): It sets parameter values on the given PreparedStatement.


2 Answers

Try

log4j.category.org.springframework.jdbc.core = TRACE

The above statement will print SQL queries with inbound parameters as well.

Incase you need to log only the query use the following

log4j.category.org.springframework.jdbc.core = DEBUG

You can enable in your logback file with the following

<logger name="org.springframework.jdbc.core.JdbcTemplate">
  <level value="debug" />
</logger>

<logger name="org.springframework.jdbc.core.StatementCreatorUtils">
  <level value="debug" />
</logger>

Update : For Springboot 2.x , it would be

logging.level.org.springframework.jdbc.core=TRACE

Thanks zhuguowei!

like image 168
CGS Avatar answered Oct 13 '22 18:10

CGS


Adding the following to your properties file also works:

logging.level.org.springframework.jdbc.core = TRACE
like image 27
Pouriya Zarbafian Avatar answered Oct 13 '22 17:10

Pouriya Zarbafian