๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Java·๏ปฟServlet·๏ปฟJSP/Library·Framework

MyBatis์™€ Log4J ์—ฐ๋™ํ•˜๊ธฐ

by Leica 2020. 2. 25.
๋ฐ˜์‘ํ˜•
  • ๊ด€๋ จ๊ธ€

- [JAVA/WEB] ์›น ํ”„๋กœ์ ํŠธ์— MyBatis ์„ธํŒ… ๋ฐ ์ ์šฉํ•˜๊ธฐ

- MyBatis ์„ค์ • ํŒŒ์ผ - SQL Mapper ์ž‘์„ฑ ๋ฐฉ๋ฒ•

- MyBatis ์„ค์ • ํŒŒ์ผ ์ž‘์„ฑ ๋ฐฉ๋ฒ•


MyBatis์™€ Log4J ์—ฐ๋™ํ•˜๊ธฐ

MyBatis์™€ log4j๋ฅผ ์—ฐ๋™ํ•˜์—ฌ MyBatis์˜ ๋กœ๊ทธ ์ถœ๋ ฅ ๊ธฐ๋Šฅ์„ ํ™œ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์ž.

MyBatis์˜ ๋กœ๊ทธ ์ถœ๋ ฅ ๊ธฐ๋Šฅ์„ ์ด์šฉํ•˜๋ฉด MyBatis์—์„œ ์‹คํ–‰ํ•˜๋Š” SQL๋ฌธ, ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’, ์‹คํ–‰ ๊ฒฐ๊ณผ๋ฅผ ์‹ค์‹œ๊ฐ„์œผ๋กœ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

ํŠนํžˆ ๋™์  SQL๋ฌธ์ด ์‹คํ–‰ ์กฐ๊ฑด์— ๋”ฐ๋ผ ์–ด๋–ป๊ฒŒ ๋‹ฌ๋ผ์ง€๋Š”์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด ๋””๋ฒ„๊น… ์‹œ ๋งค์šฐ ์œ ์šฉํ•˜๋‹ค.

 

1. MyBatis ์„ค์ •

MyBatis ์„ค์ • ํŒŒ์ผ์— ๋กœ๊ทธ ์ถœ๋ ฅ ์„ค์ •์„ ์ถ”๊ฐ€ํ•œ๋‹ค.

 

 

<configuration> ํ•˜์œ„์— <settings>๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  <setting>์„ ์‚ฌ์šฉํ•ด์„œ ๋กœ๊ทธ ์ถœ๋ ฅ๊ธฐ๋ฅผ ์ง€์ •ํ•œ๋‹ค.

MyBatis๋Š” ์—ฌ๊ธฐ์— ์ง€์ •ํ•œ ๋„๊ตฌ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋กœ๊ทธ๋ฅผ ์ถœ๋ ฅํ•˜๋‹ค.

์ด ์„ค์ •์„ ์ถ”๊ฐ€ํ•˜์ง€ ์•Š์œผ๋ฉด ๋กœ๊ทธ ์ถœ๋ ฅ ๊ธฐ๋Šฅ์€ ๋™์ž‘ํ•˜์ง€ ์•Š๋Š”๋‹ค.

 

1) name ์†์„ฑ

name์€ logImpl๋กœ ์ง€์ •ํ•œ๋‹ค.

 

2) value ์†์„ฑ

๋กœ๊ทธ ์ถœ๋ ฅ ๊ตฌํ˜„์ฒด๋ฅผ ์ง€์ •ํ•œ๋‹ค.

SLF4J, LOG4J, LOG4J2, JDK_LOGGING, COMMONS_LOGGING, STDOUT_LOGGING, NO_LOGGING๊ณผ Log ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•œ ํŠน์ • ํด๋ž˜์Šค๋ช…์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

value ์†์„ฑ๊ฐ’ ์„ค๋ช…
SLF4J SLF4J
LOG4J Log4j
LOG4J2 Log4j 2
JDK_LOGGING JDK logging
COMMONS_LOGGING Apache Commons Logging
STDOUT_LOGGING ํ‘œ์ค€ ์ถœ๋ ฅ ์žฅ์น˜๋กœ ์ถœ๋ ฅ
NO_LOGGING ๋กœ๊ทธ ์ถœ๋ ฅ ๊ธฐ๋Šฅ ์‚ฌ์šฉ ์•ˆํ•จ
ํŒจํ‚ค์ง€๋ช…์„ ํฌํ•จํ•œ ํด๋ž˜์Šค๋ช… org.apache.ibatis.logging.Log ์ธํ„ฐํŽ˜์ด์Šค์˜ ๊ตฌํ˜„์ฒด

 

์—ฌ๊ธฐ์„œ๋Š” log4j๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๋ฏ€๋กœ LOG4J๋ผ๊ณ  ์ง€์ •ํ•˜์˜€๋‹ค.

 

2. Log4J ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ค€๋น„

Log4j ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” MyBatis ์˜์กด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ํฌํ•จ๋˜์–ด ์žˆ๋‹ค.

MyBatis๋ฅผ ์„ธํŒ…ํ• ๋•Œ ํ”„๋กœ์ ํŠธ์— ์ด๋ฏธ ์ถ”๊ฐ€ํ–ˆ์„ ๊ฒƒ์ด๋‹ค.

ํ”„๋กœ์ ํŠธ dependencies(Build path)์— log4j-x.x.x.jar๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•ด๋ณด๊ณ  ์—†์œผ๋ฉด https://mvnrepository.com/artifact/log4j/log4j ์—์„œ ๋‹ค์šด๋ฐ›์•„ ์ถ”๊ฐ€ํ•˜์ž.

 

3. Log4J ์„ค์ • ํŒŒ์ผ ์ž‘์„ฑ

MyBatis์— ๋กœ๊ทธ ์ถœ๋ ฅ ์„ค์ •์„ ์ถ”๊ฐ€ํ•˜๊ณ  log4j ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ค€๋น„๊ฐ€ ์™„๋ฃŒ๋์œผ๋ฉด ๋งˆ์ง€๋ง‰์œผ๋กœ log4j ์„ค์ • ํŒŒ์ผ์„ ์ž‘์„ฑํ•ด์•ผ ํ•œ๋‹ค.

๋กœ๊ทธ ๋ ˆ๋ฒจ, ์ถœ๋ ฅ ๋ฐฉ์‹, ์ถœ๋ ฅ ํ˜•์‹, ๋กœ๊ทธ ๋Œ€์ƒ ๋“ฑ์— ๋Œ€ํ•œ ์ •๋ณด๊ฐ€ ๋“ค์–ด๊ฐ„๋‹ค.

์ž๋ฐ” ์†Œ์Šค ํด๋”(src) ํ•˜์œ„์— log4j.properties๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘์„ฑํ•œ๋‹ค.

 

log4j.properties

1
2
3
4
5
6
7
log4j.rootLogger=ERROR, stdout
 
log4j.logger.com.atoz_develop.spms.dao=DEBUG
 
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
cs

 

1) ๋กœ๊ทธ ๋ ˆ๋ฒจ ์„ค์ • - rootLogger

๋จผ์ € rootLogger = ์ตœ์ƒ์œ„ ๋กœ๊ฑฐ์˜ ๋กœ๊ทธ ๋ ˆ๋ฒจ์„ ์„ค์ •ํ•œ๋‹ค.

ํ•˜์œ„ ๋กœ๊ฑฐ๋“ค์€ ํ•ญ์ƒ ๋ถ€๋ชจ์˜ ๋กœ๊ทธ ๋ ˆ๋ฒจ์„ ์ƒ์†๋ฐ›์•„์„œ ๋ณ„๋„๋กœ ์„ค์ •ํ•˜์ง€ ์•Š์œผ๋ฉด rootLogger์˜ ๋ ˆ๋ฒจ์„ ๋˜‘๊ฐ™์ด ๋”ฐ๋ผ๊ฐ„๋‹ค.

 

ํ‘œ - ๋กœ๊ทธ ๋ ˆ๋ฒจ

๋กœ๊ทธ ๋ ˆ๋ฒจ ์„ค๋ช…
FATAL ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์ค‘์ง€ํ•ด์•ผ ํ•  ์‹ฌ๊ฐํ•œ ์˜ค๋ฅ˜
ERROR ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์ง€๋งŒ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์€ ๊ณ„์† ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ์ƒํƒœ
WARN ์ž ์žฌ์ ์ธ ์œ„ํ—˜์„ ๊ฐ–๊ณ  ์žˆ๋Š” ์ƒํƒœ
INFO ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์ฃผ์š” ์‹คํ–‰ ์ •๋ณด
DEBUG ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๋‚ด๋ถ€ ์‹คํ–‰ ์ƒํ™ฉ์„ ์ถ”์ ํ•  ์ˆ˜ ์žˆ๋Š” ์ƒ์„ธ ์ •๋ณด
TRACE DEBUG๋ณด๋‹ค ๋” ์ƒ์„ธํ•œ ์ •๋ณด

๊ฐ ๋ ˆ๋ฒจ์€ ์œ„ ๋ ˆ๋ฒจ์„ ํฌํ•จํ•ด์„œ ERROR๋กœ ์„ค์ •ํ•˜๋ฉด FATAL ๋กœ๊ทธ๋ฅผ ํฌํ•จํ•ด์„œ ์ถœ๋ ฅํ•˜๊ณ  INFO๋กœ ์„ค์ •ํ•˜๋ฉด FATAL, ERROR, WARN ๋กœ๊ทธ๋ฅผ ํฌํ•จํ•ด์„œ ์ถœ๋ ฅํ•œ๋‹ค. ๋”ฐ๋ผ์„œ TRACE๋กœ ์„ค์ •ํ•˜๋ฉด ๋ชจ๋“  ๋กœ๊ทธ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

 

2) Appender(์ถœ๋ ฅ ๋‹ด๋‹น์ž) ์„ ์–ธ

rootLogger์˜ ๋กœ๊ทธ ๋ ˆ๋ฒจ ์„ค์ • ๋‹ค์Œ์—๋Š” appender(์ถœ๋ ฅ ๋‹ด๋‹น์ž)์˜ ์ด๋ฆ„์ด ์˜จ๋‹ค. appender ์ด๋ฆ„์€ ๋งˆ์Œ๋Œ€๋กœ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๊ณ  ์ด๋ ‡๊ฒŒ ์„ ์–ธํ•œ ์ด๋ฆ„์œผ๋กœ ๋‹ค์Œ ์„ค์ •์—์„œ appender๋ฅผ ์ •์˜ํ•œ๋‹ค.

 

3) Appender(์ถœ๋ ฅ ๋‹ด๋‹น์ž) ์ •์˜

Appender๋ฅผ ์ •์˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์€

log4j.appender.stdout(appender ์ด๋ฆ„)์— ๋กœ๊ทธ๋ฅผ ์–ด๋””๋กœ ์ถœ๋ ฅํ• ์ง€,

log4j.appender.stdout.layout์—๋Š” ๋กœ๊ทธ ์ถœ๋ ฅ ํ˜•์‹์„,

log4j.appender.stdout.layout.ConversionPattern์—๋Š” ๋กœ๊ทธ๋ฅผ ์ถœ๋ ฅํ• ๋•Œ ์‚ฌ์šฉํ•  ํŒจํ„ด์„ ์ •์˜ํ•œ๋‹ค.

 

(1) ๋กœ๊ทธ๋ฅผ ์–ด๋””๋กœ ์ถœ๋ ฅํ•  ๊ฒƒ์ธ๊ฐ€

๋กœ๊ทธ๋ฅผ ๊ธฐ๋ณธ ์ถœ๋ ฅ ์žฅ์น˜์ธ ๋ชจ๋‹ˆํ„ฐ๋กœ ์ถœ๋ ฅํ•˜๊ฑฐ๋‚˜ ํŒŒ์ผ๋กœ ์ถœ๋ ฅํ•˜๊ฑฐ๋‚˜ ๋„คํŠธ์›Œํฌ๋ฅผ ์ด์šฉํ•ด์„œ ์›๊ฒฉ์˜ ์„œ๋ฒ„๋กœ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๋‹ค.

์œ„์™€ ๊ฐ™์ด ConsoleAppender๋กœ ์„ค์ •ํ•˜๋ฉด ํ‘œ์ค€ ์ถœ๋ ฅ = ๋ชจ๋‹ˆํ„ฐ๋กœ ์ถœ๋ ฅํ•œ๋‹ค.

 

์ฃผ์š” ๋กœ๊ทธ ์ถœ๋ ฅ ํด๋ž˜์Šค๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

 

ํด๋ž˜์Šค ์„ค๋ช…
org.apache.log4j.ConsoleAppender ํ‘œ์ค€ ์ถœ๋ ฅ ์žฅ์น˜์ธ ๋ชจ๋‹ˆํ„ฐ๋กœ ์ถœ๋ ฅํ•œ๋‹ค.
System.out ๋˜๋Š” System.err๋กœ ๋กœ๊ทธ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.
๊ธฐ๋ณธ์€ System.out์ด๋‹ค.
org.apache.log4j.FileAppender ํŒŒ์ผ๋กœ ๋กœ๊ทธ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.
org.apache.log4j.net.SocketAppender ์›๊ฒฉ ๋กœ๊ทธ ์„œ๋ฒ„์— ๋กœ๊ทธ ์ •๋ณด๋ฅผ ๋‹ด์€ LoggingEvent ๊ฐ์ฒด๋ฅผ ๋ณด๋‚ธ๋‹ค.

 

(2) ๋กœ๊ทธ์˜ ์ถœ๋ ฅ ํ˜•์‹

๋กœ๊ทธ๋Š” ๋ฌธ์ž์—ด, XML, HTML ํ˜•์‹์ด๋‚˜ ํŠน์ • ํŒจํ„ด์œผ๋กœ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๋‹ค.

์œ„์™€ ๊ฐ™์ด ์„ค์ •ํ•˜๋ฉด ํŠน์ • ํŒจํ„ด ํ˜•์‹์— ๋”ฐ๋ผ ๋กœ๊ทธ๋ฅผ ์ถœ๋ ฅํ•˜๋„๋ก ํ•˜๋Š”๊ฒƒ์ด๋‹ค.

์ด ๊ฒฝ์šฐ ์ถ”๊ฐ€์ ์œผ๋กœ ConversionPattern ์„ค์ •์ด ํ•„์š”ํ•˜๋‹ค.

 

๋‹ค์Œ์€ ์ฃผ์š” ์ถœ๋ ฅ ํ˜•์‹ ํด๋ž˜์Šค์ด๋‹ค.

 

ํด๋ž˜์Šค ์„ค๋ช…
org.apache.log4j.SimpleLayout ๋กœ๊ทธ ๋ ˆ๋ฒจ - ๋ฉ”์‹œ์ง€ ํ˜•์‹
org.apache.log4j.HTMLLayout HTML ํ…Œ์ด๋ธ” ํ˜•์‹
org.apache.log4j.PatternLayout ํŒจํ„ด์— ๋”ฐ๋ผ ๋กœ๊ทธ ์ถœ๋ ฅ
org.apache.log4j.xml.XMLLayout log4j.dtd ๊ทœ์น™์— ๋”ฐ๋ผ XML์„ ๋งŒ๋“ค์–ด ์ถœ๋ ฅ

 

(3) ํŒจํ„ด ์ •์˜

PatternLayout์—์„œ ์‚ฌ์šฉํ•  ํŒจํ„ด์„ ์ •์˜ํ•œ๋‹ค.

 

  • %5p : ๋กœ๊ทธ ๋ ˆ๋ฒจ์„ 5์ž๋ฆฌ ๋ฌธ์ž์—ด๋กœ ์ถœ๋ ฅํ•˜๋ผ
  • %t : thread ์ด๋ฆ„์„ ์ถœ๋ ฅํ•˜๋ผ
  • %m : ๋กœ๊ทธ ๋‚ด์šฉ์„ ์ถœ๋ ฅํ•˜๋ผ
  • %n : ๋‹ค์Œ ํ–‰

 

ConversionPattern์„ ์ •์˜ํ•˜๋Š” ๋” ์ž์„ธํ•œ ๋ฐฉ๋ฒ•์€ log4j ์‚ฌ์ดํŠธ http://logging.apache.org/log4j/ ๋ฅผ ์ฐธ๊ณ ํ•˜์ž.

 

4) ํŠน์ • ํŒจํ‚ค์ง€์˜ ํด๋ž˜์Šค์— ๋Œ€ํ•ด ๋กœ๊ทธ ๋ ˆ๋ฒจ ์„ค์ •

rootLogger์— ์ •์˜ํ•œ ๋ฃจํŠธ ๋ ˆ๋ฒจ๊ณผ ๋‹ค๋ฅธ ๋ ˆ๋ฒจ์„ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์œผ๋ฉด ํ•˜์œ„ ๋กœ๊ฑฐ๋ฅผ ์„ ์–ธํ•ด์„œ ๋กœ๊ทธ ๋ ˆ๋ฒจ์„ ์ง€์ •ํ•˜๋ฉด ๋œ๋‹ค.

 

log4.logger ๋‹ค์Œ์— ํŒจํ‚ค์ง€ ์ด๋ฆ„์„ ์ง€์ •ํ•˜๊ณ  ๋กœ๊ทธ ๋ ˆ๋ฒจ์„ ์„ค์ •ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

์œ„์™€ ๊ฐ™์ด์„ค์ •ํ•˜๋ฉด com.atoz_develop.spms.dao ํŒจํ‚ค์ง€ ํ•˜์œ„์˜ ํด๋ž˜์Šค์— ๋Œ€ํ•ด ๋กœ๊ทธ ๋ ˆ๋ฒจ์„ DEBUG๋กœ ์„ค์ •ํ•œ๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค.

 

4. ๋กœ๊ทธ ์ถœ๋ ฅ ํ…Œ์ŠคํŠธ

์„œ๋ฒ„๋ฅผ ๊ธฐ๋™ํ•˜๊ณ  ํ”„๋กœ์ ํŠธ๋ฅผ ์‹คํ–‰ํ•œ ๋’ค DB์™€ ์—ฐ๋™ํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์‹คํ–‰ํ•ด๋ณด์ž.

 

ํŒจํ„ด์„ ์„ค์ •ํ•œ ํ˜•์‹ ๋Œ€๋กœ ๋กœ๊ทธ๊ฐ€ ์ถœ๋ ฅ๋˜๋ฉฐ, MyBatis๊ฐ€ ์‹คํ–‰ํ•˜๋Š” SQL๋ฌธ๊ณผ ํŒŒ๋ผ๋ฏธํ„ฐ, ๊ฒฐ๊ณผ๋ฅผ ์ด ๋กœ๊ทธ๋ฅผ ํ†ตํ•ด ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๋กœ๊ทธ ๋ ˆ๋ฒจ์„ ๋ณ€๊ฒฝํ•˜๊ณ ์‹ถ์œผ๋ฉด ์ € ๋ถ€๋ถ„์„ ๋ณ€๊ฒฝํ•˜๋ฉด ๋œ๋‹ค.


  • ๊ด€๋ จ๊ธ€

- [JAVA/WEB] ์›น ํ”„๋กœ์ ํŠธ์— MyBatis ์„ธํŒ… ๋ฐ ์ ์šฉํ•˜๊ธฐ

- MyBatis ์„ค์ • ํŒŒ์ผ - SQL Mapper ์ž‘์„ฑ ๋ฐฉ๋ฒ•

- MyBatis ์„ค์ • ํŒŒ์ผ ์ž‘์„ฑ ๋ฐฉ๋ฒ•

 

References

์—ดํ˜ˆ๊ฐ•์˜ ์ž๋ฐ” ์›น ๊ฐœ๋ฐœ ์›Œํฌ๋ถ(ํ”„๋ฆฌ๋ ‰, 2016, ์—„์ง„์˜)

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€