본문으로 바로가기

목차

    0. 환경

    • windows10
    • openjdk version "11" 2018-09-25
    • IntelliJ Tool
    • Spring Boot 2.5.6
    • Gradle Project 
    • MyBatis + Mysql + HikariCP를 이용해 DB(Mysql) 연동할 프로젝트입니다.

    1. 의존성 추가

    • build.gradle에 Log4jdbc 의존성을 추가합니다.
    dependencies {
    	implementation 'org.bgee.log4jdbc-log4j2:log4jdbc-log4j2-jdbc4.1:1.16'
    }

    2. log4jdbc.log4j2.properties 생성

    • resources/log4jdbc.log4j2.properties
    log4jdbc.spylogdelegator.name=net.sf.log4jdbc.log.slf4j.Slf4jSpyLogDelegator
    log4jdbc.dump.sql.maxlinelength=0

    3. log4j2.xml 추가 설정

    • 로그에 쿼리를 출력하기 위해 log4j2.xml 파일에 추가로 입력합니다.
        <Loggers>
            <!-- jdbc.sql Loggers -->
            <Logger name="jdbc.connection"      level="OFF" additivity="false"><AppenderRef ref="console" /></Logger>
            <Logger name="jdbc.audit"           level="WARN" additivity="false"><AppenderRef ref="console" /></Logger>
            <Logger name="jdbc.sqlonly"         level="OFF" additivity="false"><AppenderRef ref="console" /></Logger>
            <Logger name="jdbc.sqltiming"       level="DEBUG" additivity="false"><AppenderRef ref="console" /></Logger>
            <Logger name="jdbc.resultset"       level="OFF" additivity="false"><AppenderRef ref="console" /></Logger>
            <Logger name="jdbc.resultsettable"  level="DEBUG" additivity="false"><AppenderRef ref="console" /></Logger>
    
            <!-- FATAL, ERROR, WARN, INFO, DEBUG, TRACE -->
            <Root level="INFO">
                <AppenderRef ref="console" />
                <AppenderRef ref="RollingFile" />
            </Root>
        </Loggers>

    4. DB 연결 설정 정보 수정

    • application.properties에 설정한 driver-class-name와 jdbc-url를 수정합니다.
    #spring.datasource.hikari.driver-class-name=com.mysql.cj.jdbc.Driver
    #spring.datasource.hikari.jdbc-url=jdbc:mysql://localhost:3306/test?useSSL=false&amp&serverTimezone=UTC
    
    spring.datasource.hikari.driver-class-name=net.sf.log4jdbc.sql.jdbcapi.DriverSpy
    spring.datasource.hikari.jdbc-url=jdbc:log4jdbc:mysql://localhost:3306/test?useSSL=false&amp&serverTimezone=UTC
    spring.datasource.hikari.username=root
    spring.datasource.hikari.password=password

    5. 테스트