본문 바로가기

Spring

p6spy를 이용한 쿼리 로깅

개발 환경에서 쿼리 추적과 파라미터 확인을 위해 사용한 p6spy 적용 방법을 정리해보았다.

 

 


maven 프로젝트

 

 

1. dependency 추가

 pom.xml에 의존성을 추가한다.

<dependency>
  <groupId>p6spy</groupId>
  <artifactId>p6spy</artifactId>
  <version>3.8.2</version>
</dependency>

 

 

 

2. logback-spring.xml 설정

 logback은 spring boot에 포함된 기본 로깅 프레임워크로서 콘솔 출력을 위해 해당 파일 생성 후 설정값을 입력해준다.

 

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>
                %d{HH:mm:ss.SSS} [%thread] %-5level  %logger{36} - %msg%n
            </pattern>
        </encoder>
    </appender>

    <root level="DEBUG">
        <appender-ref ref="STDOUT" />
    </root>
</configuration>

 

 

 

3. spy.properties 설정

 logback-spring.xml과 동일하게 resources 디렉토리에 생성해준다.

driverlist=oracle.jdbc.driver.OracleDriver
appender=com.p6spy.engine.spy.appender.Slf4JLogger
logMessageFormat=com.p6spy.engine.spy.appender.MultiLineFormat
filter=true
exclude=order

 

-driverlist : DriverManager에 등록할 jdbc 드라이버 클래스 목록

-appender: 로그 정보가 출력되는 위치

-logMessageFormat: 로그 메시지 형식 

-filter: include, exclude 속성을 사용하기 위해서는 true로 설정

-exclude: 특정 문자열이 있는 명령문을 제외하고 로그를 출력

 

 

이 외의 설정 파일의 전체 내용은 p6spy document Common Property File Settings 에서 확인할 수 있다.

 

https://p6spy.readthedocs.io/en/latest/configandusage.html#common-property-file-settings

 

Configuration and Usage — p6spy 3.9.2-SNAPSHOT documentation

An example spy.properties file follows (please note default values mentioned as these refer to defaults mentioned in section: Configuration and Usage): modulelist modulelist holds the list of p6spy modules activated. A module contains a group of functional

p6spy.readthedocs.io