Framework2018. 10. 12. 09:28

Spring + Mybatis + Log4j 환경에서 SQL 로그 출력

환경에 따라 다를 수 있으니 참고만 하세요!

1. pom.xml

log4jdbc-remix 추가

 

2. log4j.xml 수정

~/src/main/resources/log4j.xml

    <!-- Query Loggers -->
    <logger name="jdbc.sqlonly" additivity="false"> 
        <level value="INFO"/> 
        <appender-ref ref="console"/> 
    </logger>     

    <logger name="jdbc.resultsettable" additivity="false"> 
        <level value="INFO"/> 
        <appender-ref ref="console"/> 
    </logger>

    <!-- Root Logger --> 

 

3. mybatis-context.xml 수정

<!-- Root Context: defines shared resources visible to all other web components -->
 <context:property-placeholder location="/WEB-INF/spring/mybatis/jdbc.properties" />
 <bean id ="dataSourceSpied" class= "org.springframework.jdbc.datasource.SimpleDriverDataSource" >
            <property name ="driverClass" value= "${jdbc.driverClass}"></property >
            <property name ="url" value= "${jdbc.url}" ></property >
            <property name ="username" value="${jdbc.username}"></property>
            <property name ="password" value= "${jdbc.password}"></property >

     </bean>    

     <!-- Log4JDBC 설정 -->
     <bean id="dataSource" class="net.sf.log4jdbc.Log4jdbcProxyDataSource"> 
      <constructor-arg ref="dataSourceSpied" /> 
      <property name="logFormatter"> 
      <bean class="net.sf.log4jdbc.tools.Log4JdbcCustomFormatter"> 
      <property name="loggingType" value="MULTI_LINE" /> 
      <property name="sqlPrefix" value="SQL:::" /> 
      </bean> 
      </property> 
     </bean>

Posted by 비니미니파파