title: How to log spring JdbcTemplate sql queries and DB response in log file using log4j2
body: <p>I am using spring org.springframework.jdbc.core.JdbcTemplate and org.springframework.jdbc.core.simple.SimpleJdbcCall for my database queries. I am using log4j2.xml for logging. I want to log all my sql queries and their DB result in db.log file.</p>
  
  <p>I have tried to used following logger in my log4j2.xml file but it did not log anything in db.log file. I tried level="TRACE" and level="debug" but both not working.</p>
  
  <pre><code>    &lt;RollingFile name="db" fileName="${sys:catalina.home}/logs/db.log"
          filePattern="${sys:catalina.home}/logs/$${date:yyyy-MM-dd}/db-%d{yyyy-MM-dd}-%i.log.gz"&gt;
          &lt;PatternLayout
              pattern="%d{dd/MM/yyyy HH:mm:ss,SSS} [%X{cartID}] [%X{sessionId}] [%p] [%t] [%c] (%F:%L)  - %m%n" /&gt;
          &lt;Policies&gt;
              &lt;TimeBasedTriggeringPolicy interval="1"
                  modulate="true" /&gt;
              &lt;SizeBasedTriggeringPolicy size="10 MB" /&gt;
          &lt;/Policies&gt;
      &lt;/RollingFile&gt;
  &lt;/Appenders&gt;
  &lt;Loggers&gt;
      &lt;Logger name="org.springframework.jdbc.core.JdbcTemplate" level="TRACE" additivity="false"&gt;
          &lt;Appender-Ref ref="db" /&gt;
      &lt;/Logger&gt;
  </code></pre>
  
  <p>In our java classes we are using following sql </p>
  
  <pre><code>String sQuery = "select count(*) from impersonation_requests where ir_eid = ? and  ir_tmp_userid = ?";
  String value =  template
                  .queryForObject(sQuery, new Object[] { passwordInfo.getEid(),
                          passwordInfo.getUserId() }, String.class);
  </code></pre>
  
  <p>Here var template is instance variable of org.springframework.jdbc.core.JdbcTemplate</p>
  
  <p>I want to see sQuery and value  entries in my db.log file.
  Can we achieve this using JdbcTemplate or I need to implement logger in all my DAO classes and log sQuery and values in each class where I am using JdbcTemplate. I want to avoid this approach. Please suggest.</p>