2016년 10월 17일 월요일

MyBatis 쿼리로그 출력 및 정렬 설정

1. pom.xml 에 추가한다.


<dependency>
    <groupId>org.lazyluke</groupId>
    <artifactId>log4jdbc-remix</artifactId>
    <version>0.2.7</version>
</dependency>

2. log4j.xml 을 수정한다.


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
 
    <!-- Appenders -->
    <appender name="console" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d %5p [%c] %m%n" />
        </layout>   
    </appender>
     
    <appender name="console-infolog" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d %5p %m%n" />
        </layout>   
    </appender>
     
    <!-- Application Loggers -->
    <logger name="place" additivity="false">
        <level value="debug" />
        <appender-ref ref="console"/>
    </logger>
     
    <!-- Query Loggers -->
    <logger name="jdbc.sqlonly" additivity="false">  
        <level value="INFO"/>  
        <appender-ref ref="console-infolog"/>  
    </logger>
     
    <logger name="jdbc.resultsettable" additivity="false">  
        <level value="INFO"/>  
        <appender-ref ref="console"/>  
    </logger>  
 
    <!-- Root Logger -->
    <root>
        <priority value="off"/>
        <appender-ref ref="console" />
    </root>
     
</log4j:configuration>

3. context-datasource.xml 을 수정한다.


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                        http://www.springframework.org/schema/jdbc  http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd">
    <!-- Connection Pool --> 
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:포트/데이터베이스명"/>
        <property name="username" value="아이디"/>
        <property name="password" value="비밀번호"/>
    </bean>
    
    <bean id="dataSourceLog4j" class="net.sf.log4jdbc.Log4jdbcProxyDataSource">
        <constructor-arg ref="dataSource" />
        <property name="logFormatter">
            <bean class="net.sf.log4jdbc.tools.Log4JdbcCustomFormatter">
                <property name="loggingType" value="MULTI_LINE" />
                <property name="sqlPrefix" value="SQL         :  "/>
            </bean>
        </property>
    </bean>
    
</beans>

4. console을 확인한다.





Share:

0 개의 댓글:

댓글 쓰기