
Spring Boot + MyBatis 쿼리 자동 로깅 라이브러리 개발기
이번 포스팅에서는 내가 처음으로 만들어 본 오픈소스 라이브러리인 MyBatis Query Logger를 소개 하려고 한다. 실무에서 MyBatis를 사용하면서 쿼리와 파라미터를 로깅하는 데 번거로움을 느꼈고, 이를 해결하고자 인터셉터 기반의 로깅 도구를 직접 제작하게 되었다. 🤔
라이브러리 소개
MyBatis Query Logger는 다음과 같은 기능을 제공한다:
- SQL 쿼리 자동 로깅
- 바인딩된 파라미터 이름 및 값 출력
- 실행 시간(ms) 측정 및 느린 쿼리(Slow Query) 감지
- SLF4J 또는 System.out.println 중 선택 가능
- Spring Boot 자동 설정 지원
설치 방법
dependencies {
implementation 'com.github.DongJu-Na:mybatis-query-logger:0.1.4'
}
# application.yml 설정 예시
mybatis-query-logger:
enabled: true
use-slf4j: true
slow-query-threshold-ms: 1000
사용 예시
다음과 같이 출력된다:
====== MyBatisQueryLogger ======
SQL:
SELECT * FROM USERS WHERE ID = ?
PARAMETERS:
id = 1 (Integer)
DURATION: 5ms
================================
느린 쿼리 감지
설정한 임계값(slow-query-threshold-ms)을 초과할 경우 다음 메시지가 함께 출력된다:
SLOW QUERY DETECTED! Threshold: 1000ms
구현 포인트
- @Intercepts 어노테이션을 통해 MyBatis StatementHandler의 prepare 메서드를 감쌌다.
- BoundSql 객체를 활용하여 SQL 문자열과 바인딩된 파라미터 목록을 추출한다.
- 파라미터 타입은 Map, DTO, null 등 다양한 상황을 고려하여 리플렉션 기반으로 추출하고 있다.
- 로깅 여부와 출력 방식은 application.yml 설정으로 제어할 수 있다.
배운 점 및 느낀 점
Spring Boot에서의 자동 설정 구조, MyBatis 인터셉터 구조, JitPack을 통한 Maven 배포 등 다양한 요소를 처음 다뤄봤다. 실제 현업에서도 유용하게 쓸 수 있고, 다른 개발자들도 편하게 사용할 수 있도록 문서화와 설정 확장에도 신경을 썼다.
앞으로의 개선 계획
- 로그 출력 포맷 개선 (멀티라인 → 단일라인 등)
- 파라미터 JSON 포맷 출력 옵션
- LogLevel 설정 기능 추가
마무리하며
누군가에겐 사소할 수 있지만 실무에서는 매우 유용한 기능이라고 생각한다.
라이브러리로써도, 오픈소스 커리어 시작점으로써도 의미 있는 작업이었고,
앞으로도 계속 유지보수하고 기능을 확장해나갈 계획이다.
궁금한 점이나 버그 제보는 언제든 GitHub Issue
GitHub 저장소: https://github.com/DongJu-Na/mybatis-query-logger
Build Log : https://jitpack.io/#DongJu-Na/mybatis-query-logger/0.1.4
함께 읽어볼만한 게시물들..
MyBatis 공식 문서 - 플러그인 → @Intercepts, @Signature 어노테이션 설명 포함
https://mybatis.org/mybatis-3/ko/configuration.html#plugins
마이바티스 3 | 매퍼 설정 – mybatis
매퍼 설정 마이바티스 XML 설정파일은 다양한 설정과 프로퍼티를 가진다. 문서의 구조는 다음과 같다.: configuration properties 이 설정은 외부에 옮길 수 있다. 자바 프로퍼티 파일 인스턴스에 설정할
mybatis.org
https://mybatis.org/mybatis-3/ko/java-api.html#BoundSql
마이바티스 3 | 자바 API – mybatis
자바 API 이제 마이바티스를 설정하는 방법과 매핑을 만드는 방법을 알게 되었다. 이미 충분히 잘 사용할 준비가 된 셈이다. 마이바티스 자바 API 는 당신의 노력에 대한 보상을 얻게 할 것이다. JD
mybatis.org
MyBatis Spring Boot Starter 문서 → ConfigurationCustomizer 등록 방식
https://mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/
Introduction – mybatis-spring-boot-autoconfigure
Using the SpringBootVFS The MyBatis-Spring-Boot-Starter provides the SpringBootVFS as an implementation class of VFS. The VFS is used for searching classes (e.g. target class of type alias, type handler class) from an application (or application server). I
mybatis.org
Spring Boot 공식 문서 - Externalized Configuration
https://docs.spring.io/spring-boot/appendix/application-properties/index.html
Common Application Properties :: Spring Boot
docs.spring.io
https://jitpack.io/docs/#gradle
:: Documentation for JitPack.io
JitPack.io JitPack is a novel package repository for JVM and Android projects. It builds Git projects on demand and provides you with ready-to-use artifacts (jar, aar). If you want your library to be available to the world, there is no need to go through p
docs.jitpack.io
https://www.slf4j.org/docs.html
SLF4J Documentation
Documentation Given the small size of SLF4J, its documentation is not very lengthy. Videos
www.slf4j.org