본문 바로가기
Spring

Spring 4.3.2 log4jdbc-log4j2 설정 방법

by 수앙 2016. 9. 29.

1. pom.xml

자바 1.8 기준 메이븐 디펜던시 설정

<dependency>
	<groupId>org.bgee.log4jdbc-log4j2</groupId>
	<artifactId>log4jdbc-log4j2-jdbc4.1</artifactId>
	<version>1.16</version>
</dependency>

 

2. DataSource 설정

DBCP는 HikariCP를 사용(자바 기반 설정)

@Bean
public DataSource dataSource() {
	HikariDataSource ds = new HikariDataSource();
	ds.setMaximumPoolSize(20);
	ds.setDriverClassName("net.sf.log4jdbc.sql.jdbcapi.DriverSpy");
	ds.setJdbcUrl("jdbc:log4jdbc:mariadb://localhost:3306/db");
	ds.addDataSourceProperty("user", "root");
	ds.addDataSourceProperty("password", "password");
	ds.setAutoCommit(false);
	return ds;
}

 

3. log4jdbc.log4j2.properties 설정

src/main/resources 경로에 log4jdbc.log4j2.properties 파일 생성 후 설정

log4jdbc.drivers=org.mariadb.jdbc.Driver
log4jdbc.auto.load.popular.drivers=false
log4jdbc.spylogdelegator.name=net.sf.log4jdbc.log.slf4j.Slf4jSpyLogDelegator
log4jdbc.dump.sql.maxlinelength=0

 

4. log4j2.xml

<logger name="log4jdbc.log4j2" level="error" additivity="false">
	<appender-ref ref="Console" />
</logger>

<Logger name="jdbc.sqlonly" level="off" />

<Logger name="jdbc.sqltiming" level="info" />

<Logger name="jdbc.audit" level="off" />

<Logger name="jdbc.resultset" level="off" />

<Logger name="jdbc.resultsettable" level="info" />

<Logger name="jdbc.connection" level="off" />

 

5. 결과

20:10:34.376 [http-nio-8080-exec-1] INFO  jdbc.sqltiming - /* memberDao.list : 회원 목록 조회 */
        SELECT
                M.MEMBER_ID
              , M.MEMBER_NAME
              , C.CONTENTS_ID
              , C.CONTENTS_NAME
        FROM
                MEMBER M
        INNER JOIN
                MEMBER_CONTENTS MC
        ON      MC.MEMBER_ID = M.MEMBER_ID
        INNER JOIN
                CONTENTS C
        ON      C.CONTENTS_ID = MC.CONTENTS_ID
 {executed in 25 msec}
20:10:34.423 [http-nio-8080-exec-1] INFO  jdbc.resultsettable - 
|-------------------------------------|------------|------------|--------------|
|member_id                            |member_name |contents_id |contents_name |
|-------------------------------------|------------|------------|--------------|
|ca470654-84bb-11e6-954f-00e04ca217d9 |홍길동      |[unread]    |[unread]      |
|ca470654-84bb-11e6-954f-00e04ca217d9 |홍길동      |[unread]    |[unread]      |
|-------------------------------------|------------|------------|--------------|

 

끝.

'Spring' 카테고리의 다른 글

Spring ehcache 설정  (0) 2016.12.12
Spring 4 자바 기반 Thymeleaf 설정  (0) 2016.10.16
Springfox 설정  (0) 2016.09.25
Spring 4.3.2 자바 annotation 기반 설정  (0) 2016.09.21
Spring batch 예제  (0) 2016.04.20

댓글