pom.xml (기존 파일 추가)
<!-- SQL 로거 -->
<dependency>
<groupId>org.bgee.log4jdbc-log4j2</groupId>
<artifactId>log4jdbc-log4j2-jdbc4.1</artifactId>
<version>1.16</version>
</dependency>
src/main/resources/application.yml (기존 파일 수정)
# driver-class-name: com.mysql.cj.jdbc.Driver
driver-class-name: net.sf.log4jdbc.sql.jdbcapi.DriverSpy
url: jdbc:log4jdbc:mysql://127.0.0.1:3306/db이름?useUnicode=true&characterEncoding=utf8&autoReconnect=true&serverTimezone=Asia/Seoul&useOldAliasMetadataBehavior=true&zeroDateTimeNehavior=convertToNull
src/main/resources/log4jdbc.log4j2.properties (파일 생성)
log4jdbc.spylogdelegator.name = net.sf.log4jdbc.log.slf4j.Slf4jSpyLogDelegator
log4jdbc.dump.sql.maxlinelength = 0
src/main/resources/logback-spring.xml (파일 생성)
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<springProperty name="LOG_PATH" source="custom.logging.dir" />
<!-- aplication.yml에 정의한 log level, dir 데이터를 정의합니다 -->
<springProperty name="LOG_LEVEL" source="custom.logging.level" />
<!--콘솔에 로그를 남깁니다. -->
<appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} [%thread] [%-5level] %logger{36} -
%msg%n
</Pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
<!--파일에 로그를 남깁니다. -->
<appender name="File" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/today_common.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d{yyyy-MM-dd HH:mm:ss} [%-5level] %logger{36} - %msg%n
</Pattern>
<charset>UTF-8</charset>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_PATH}/daily_common/%d{yyyy-MM-dd}.log
</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
</appender>
<!--파일에 로그를 남깁니다. (Roling) -->
<appender name="RollingFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/archived/today_common.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d{yyyy-MM-dd HH:mm:ss} [%-5level] %logger{36} - %msg%n
</Pattern>
<charset>UTF-8</charset>
</encoder>
<!-- 10MB가 넘어가면 새로운 압축파일이 생성 -->
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>10MB</MaxFileSize>
</triggeringPolicy>
<!-- 파일을 덮어쓰는 정책 -->
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<FileNamePattern>${LOG_PATH}/archived/today_common.%i.log.zip
</FileNamePattern>
<!-- 10MB단위로 로그 압축 파일이 생성된다. (1 ~ 10) 10개가 넘어가면 오래된순으로 덮어쓰기 됩니다. -->
<MinIndex>1</MinIndex>
<MaxIndex>10</MaxIndex>
</rollingPolicy>
</appender>
<!--에러 일 경우 파일에 로그를 남깁니다 -->
<appender name="Error" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>error</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<file>${LOG_PATH}/error.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} [%-5level] %logger{36} - %msg%n
</Pattern>
<charset>UTF-8</charset>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_PATH}/daily_error/%d{yyyy-MM-dd}.log
</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
</appender>
<!-- org.springframework.web 패키지 아래에서 발생하는 INFO 레벨 로그를 Console, File에 남깁니다. root의 속성을 상속받지 않습니다. -->
<logger name="org.springframework.web" level="INFO" additivity="false">
<appender-ref ref="Console" />
<appender-ref ref="File" />
<appender-ref ref="Error" />
</logger>
<logger name="org.apache.ibatis" level="DEBUG" additivity="false">
<appender-ref ref="Console" />
<appender-ref ref="RollingFile" />
<appender-ref ref="Error" />
</logger>
<!-- DEBUG 레벨 로그를 Console, File, RollingFile에 남깁니다. ERROR 인 경우 bamdule-error.log에 남깁니다. -->
<root level="${LOG_LEVEL}">
<appender-ref ref="Console" />
<appender-ref ref="File" />
<appender-ref ref="Error" />
</root>
<logger name="jdbc" level="OFF" />
<logger name="jdbc.sqlonly" level="OFF" />
<logger name="jdbc.sqltiming" level="DEBUG" />
<logger name="jdbc.audit" level="OFF" />
<logger name="jdbc.resultset" level="OFF" />
<logger name="jdbc.resultsettable" level="DEBUG" />
<logger name="jdbc.connection" level="OFF" />
</configuration>