본문 바로가기

개발중/Spring

Spring Project 의 MyBatis 쿼리를 Console 에 찍는 설정 (log4j2)

728x90
반응형

 

의존성 추가

- 로그 관련 라이브러리를  추가합니다.

 

<!-- 로그 설정 -->
<dependency>
    <groupId>org.bgee.log4jdbc-log4j2</groupId>
    <artifactId>log4jdbc-log4j2-jdbc4.1</artifactId>
    <version>1.16</version>
</dependency>

 

 

log4j2 파일

- resource 하위에 log4j2.xml 파일을 생성합니다.

 

 

 

- 파일 안에 아래와 같이 코드를 작합니다.

 

<Configuration status="WARN">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %msg%n"/>
        </Console>
    </Appenders>
    <Loggers>
        <Root level="INFO">
            <AppenderRef ref="Console"/>
        </Root>
    </Loggers>
</Configuration>

 

 

DataBase 접속 정보에 대한 수정

 

변경 전

 

spring.datasource.hikari.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.hikari.jdbc-url=jdbc:mysql://localhost:3306/binsoo_db?serverTimezone=UTC&characterEncoding=UTF-8
spring.datasource.hikari.username=root
spring.datasource.hikari.password=1234
spring.datasource.hikari.pool-name=TestHikariPool

 

변경 후

 

spring.datasource.hikari.driver-class-name=net.sf.log4jdbc.sql.jdbcapi.DriverSpy
spring.datasource.hikari.jdbc-url=jdbc:log4jdbc:mysql://localhost:3306/binsoo_db?serverTimezone=UTC&characterEncoding=UTF-8
spring.datasource.hikari.username=root
spring.datasource.hikari.password=1234
spring.datasource.hikari.pool-name=TestHikariPool

 

변경사항

 

 

 

결과 확인

 

2023-12-30 11:23:01.812  INFO 22876 --- [nio-8080-exec-1] log4jdbc.log4j2                          : 1. Connection.prepareStatement(SELECT id AS seq
		       , userName AS name
		  FROM BINSOO_USER) returned net.sf.log4jdbc.sql.jdbcapi.PreparedStatementSpy@25ae6149
2023-12-30 11:23:01.825  INFO 22876 --- [nio-8080-exec-1] log4jdbc.log4j2                          : 1. SELECT id AS seq , userName AS name FROM BINSOO_USER  {executed in 9 ms}
2023-12-30 11:23:01.825  INFO 22876 --- [nio-8080-exec-1] log4jdbc.log4j2                          : 1. PreparedStatement.execute() returned true
2023-12-30 11:23:01.828  INFO 22876 --- [nio-8080-exec-1] log4jdbc.log4j2                          : 1. ResultSet.new ResultSet returned 
2023-12-30 11:23:01.828  INFO 22876 --- [nio-8080-exec-1] log4jdbc.log4j2                          : 1. PreparedStatement.getResultSet() returned net.sf.log4jdbc.sql.jdbcapi.ResultSetSpy@3fd51580
2023-12-30 11:23:01.830  INFO 22876 --- [nio-8080-exec-1] log4jdbc.log4j2                          : 1. ResultSet.getMetaData() returned com.mysql.cj.jdbc.result.ResultSetMetaData@2dad1813 - Field level information: 
	com.mysql.cj.result.Field@47cdd5c1[dbName=binsoo_db,tableName=BINSOO_USER,originalTableName=binsoo_user,columnName=seq,originalColumnName=id,mysqlType=3(FIELD_TYPE_INT),sqlType=4,flags=, charsetIndex=63, charsetName=ISO-8859-1]
	com.mysql.cj.result.Field@4bb80ae4[dbName=binsoo_db,tableName=BINSOO_USER,originalTableName=binsoo_user,columnName=name,originalColumnName=userName,mysqlType=253(FIELD_TYPE_VARCHAR),sqlType=12,flags=, charsetIndex=33, charsetName=UTF-8]
2023-12-30 11:23:01.831  INFO 22876 --- [nio-8080-exec-1] log4jdbc.log4j2                          : 1. ResultSet.getType() returned 1003
2023-12-30 11:23:01.831  INFO 22876 --- [nio-8080-exec-1] log4jdbc.log4j2                          : 1. ResultSet.isClosed() returned false
2023-12-30 11:23:01.831  INFO 22876 --- [nio-8080-exec-1] log4jdbc.log4j2                          : 1. ResultSet.next() returned true
2023-12-30 11:23:01.835  INFO 22876 --- [nio-8080-exec-1] log4jdbc.log4j2                          : 1. ResultSet.getInt(seq) returned 1
2023-12-30 11:23:01.835  INFO 22876 --- [nio-8080-exec-1] log4jdbc.log4j2                          : 1. ResultSet.getString(name) returned bin
2023-12-30 11:23:01.837  INFO 22876 --- [nio-8080-exec-1] log4jdbc.log4j2                          : 1. ResultSet.getInt(seq) returned 1
2023-12-30 11:23:01.837  INFO 22876 --- [nio-8080-exec-1] log4jdbc.log4j2                          : 1. ResultSet.getString(name) returned bin
2023-12-30 11:23:01.837  INFO 22876 --- [nio-8080-exec-1] log4jdbc.log4j2                          : 1. ResultSet.isClosed() returned false
2023-12-30 11:23:01.837  INFO 22876 --- [nio-8080-exec-1] log4jdbc.log4j2                          : 1. ResultSet.next() returned true
2023-12-30 11:23:01.837  INFO 22876 --- [nio-8080-exec-1] log4jdbc.log4j2                          : 1. ResultSet.getInt(seq) returned 2
2023-12-30 11:23:01.837  INFO 22876 --- [nio-8080-exec-1] log4jdbc.log4j2                          : 1. ResultSet.getString(name) returned bim
2023-12-30 11:23:01.837  INFO 22876 --- [nio-8080-exec-1] log4jdbc.log4j2                          : 1. ResultSet.getInt(seq) returned 2
2023-12-30 11:23:01.837  INFO 22876 --- [nio-8080-exec-1] log4jdbc.log4j2                          : 1. ResultSet.getString(name) returned bim
2023-12-30 11:23:01.837  INFO 22876 --- [nio-8080-exec-1] log4jdbc.log4j2                          : 1. ResultSet.isClosed() returned false
2023-12-30 11:23:01.838  INFO 22876 --- [nio-8080-exec-1] log4jdbc.log4j2                          : 
|---|---------|
|id |username |
|---|---------|
|1  |bin      |
|2  |bim      |
|---|---------|
728x90
반응형