log4jdbc 설정 추가하기
log4jdbc.log4j2.properties
파일을 src/main/resources
디렉토리에 생성합니다.
#################################### # Log4JDBC Configuration # http://log4jdbc.brunorozendo.com/ #################################### log4jdbc.spylogdelegator.name=net.sf.log4jdbc.log.slf4j.Slf4jSpyLogDelegator
JDBC Driver 및 URL 설정하기
이제 src/main/resources/application.yml
파일에서 다음과 같이 DataSource 부분에 jdbc-url
과 driver-class-name
부분을 log4jdbc가 동작할 수 있도록 변경합니다.
spring: application: name: Hello World ################### ## JDBC ################### datasource: hikari: jdbc-url: jdbc:log4jdbc:postgresql://localhost:5432/test username: postgres password: postgres driver-class-name: net.sf.log4jdbc.sql.jdbcapi.DriverSpy connection-test-query: SELECT 1 maximum-pool-size: 10 minimum-idle: 3 sql-script-encoding: UTF-8 continue-on-error: true initialization-mode: always
log4jdbc 추가하기
이제 log4jdbc를 mvnrepository.com에서 검색하여 다음과 같이 pom.xml
파일에 추가합니다.
<dependency> <groupId>org.bgee.log4jdbc-log4j2</groupId> <artifactId>log4jdbc-log4j2-jdbc4.1</artifactId> <version>${log4jdbc.version}</version> </dependency>
Logging 설정하기
이제 마지막으로 src/main/resources/application.yml
파일의 제일 하단에 다음과 같이 로깅 설정을 추가합니다. JDBC 쿼리와 실행 시간 등을 로그로 생성하기 위해서 다음과 같이 jdbc.*
로 시작하는 속성의 로깅 레벨을 INFO
로 변경하면 로그가 생성됩니다.
################### ## Logging ################### logging: level: root: INFO tomcat: INFO freemarker: INFO jdbc.connection: WARN jdbc.audit: WARN jdbc.sqlonly: WARN jdbc.resultset: WARN jdbc.resultsettable: DEBUG org.hibernate: INFO org.springframework: INFO org.springframework.session: WARN io.datadynamics: DEBUG org.apache: INFO com.zaxxer: INFO group: tomcat: org.apache.catalina, org.apache.coyote, org.apache.tomcat
Logback XML 파일로 설정하려면 다음과 같이 작성합니다.
<?xml version="1.0" encoding="UTF-8"?> <configuration> <conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" /> <conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" /> <conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" /> <property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} [%X{requestId:- }]%clr(:){faint} [%X{invocationId:- }] %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/> <property name="CONSOLE_LOG_CHARSET" value="${CONSOLE_LOG_CHARSET:-default}"/> <property name="FILE_LOG_PATTERN" value="${FILE_LOG_PATTERN:-%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}} ${LOG_LEVEL_PATTERN:-%5p} ${PID:- } --- [%t] %-40.40logger{39} : %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/> <property name="FILE_LOG_CHARSET" value="${FILE_LOG_CHARSET:-default}"/> <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>${CONSOLE_LOG_PATTERN}</pattern> <charset>${CONSOLE_LOG_CHARSET}</charset> </encoder> </appender> <logger name="org.hibernate.validator.internal.util.Version" level="WARN"/> <logger name="org.apache.coyote.http11.Http11NioProtocol" level="WARN"/> <logger name="org.crsh.plugin" level="WARN"/> <logger name="org.apache.tomcat.util.net.NioSelectorPool" level="WARN"/> <logger name="org.apache.catalina.startup.DigesterFactory" level="ERROR"/> <logger name="org.springframework" level="INFO"/> <logger name="org.hibernate" level="INFO"/> <logger name="org.quartz" level="INFO"/> <logger name="com.mchange" level="INFO"/> <logger name="com.zaxxer" level="WARN"/> <logger name="springfox" level="WARN"/> <logger name="springfox.documentation" level="INFO"/> <logger name="jdbc.sqlonly" level="WARN"/> <logger name="jdbc.sqltiming" level="WARN"/> <logger name="jdbc.resultset" level="WARN"/> <logger name="jdbc.audit" level="WARN"/> <logger name="jdbc.connection" level="WARN"/> <logger name="io.datadynamics" level="DEBUG"/> <root level="DEBUG"> <appender-ref ref="CONSOLE"/> </root> </configuration>