๐Ÿ’ป BackEnd/๐ŸŸข Spring | SpringBoot

๐ŸŸข [SpringBoot] [Mybatis] Mybatis Log ์„ค์ •

Dbswnstjd 2023. 9. 20. 10:05

๐Ÿšฉ Mybatis Log ์„ค์ •

Mybatis๋ฅผ ์‚ฌ์šฉํ•˜๋ฉฐ ๊ฐœ๋ฐœํ•˜๋‹ค ๋ณด๋ฉด SQL์ด ์–ด๋–ป๊ฒŒ ๋Œ์•„๊ฐ€๋Š”์ง€ ์•Œ์•„์•ผ ํ•  ๋•Œ๊ฐ€ ์žˆ๋‹ค. SQL์˜ WHERE์ ˆ์—์„œ if ์กฐ๊ฑด์„ ํ™•์ธํ•˜๊ณ  ์‹ถ์–ด ๋กœ๊ทธ๋ฅผ ์ฐพ์•„๋ณด๋‹ค๊ฐ€ ์„ค์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์„ค๋ช…ํ•˜๋ ค๊ณ  ํ•œ๋‹ค. 

 

1. Pom.xml ์˜์กด์„ฑ ์ถ”๊ฐ€

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

๋จผ์ € pom.xml์— ์˜์กด์„ฑ์„ ์ถ”๊ฐ€ํ•ด์ค˜์•ผ ํ•œ๋‹ค. ์™œ์ธ์ง€๋Š” ๋ชจ๋ฅด๊ฒ ๋Š”๋ฐ 4๊ฐ€ ๋˜๋Š” ํ”„๋กœ์ ํŠธ๊ฐ€ ์žˆ๊ณ  4.1์ด ๋˜๋Š” ํ”„๋กœ์ ํŠธ๊ฐ€ ์žˆ๋‹ค. ์ด๊ฒƒ์„ ์ž์‹ ์˜ ํ”„๋กœ์ ํŠธ์— ๋งž๋Š” ๋ฒ„์ „์„ ์‚ฌ์šฉํ•˜๋„๋ก ํ•œ๋‹ค. 

 

2. Application.properties 

spring.datasource.driver-class-name=net.sf.log4jdbc.sql.jdbcapi.DriverSpy
jdbc:log4jdbc:oracle:thin:@์ž์‹ ์˜IP:xe
log4jdbc.spylogdelegator.name=net.sf.log4jdbc.log.slf4j.Slf4jSpyLogDelegator
log4jdbc.dump.sql.maxlinelength=0

logging.level.jdbc.sqlonly=off
logging.level.jdbc.sqltiming=info
logging.level.jdbc.resultsettable=info
logging.level.jdbc.audit=off
logging.level.jdbc.resultset=off
logging.level.jdbc.connection=off

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋Š” Oracle์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์–ด MySQL์ด๋‚˜ MSSQL์€ ์กฐ๊ธˆ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ๋‹ค.

/resource ์•ˆ์— ์žˆ๋Š” ์„ค์ • ํŒŒ์ผ์— ์œ„์˜ ๋‚ด์šฉ๋“ค์„ IP๋ถ€๋ถ„์„ ๋ฐ”๊ฟ”์„œ ์ถ”๊ฐ€ํ•ด ์ค€๋‹ค. 

Log Level์„ ๋ฐ”๊พธ๋ ค๋ฉด logging.level=info ์ด๋ ‡๊ฒŒ ๋ฐ”๊พธ๋ฉด ๋œ๋‹ค.