본문 바로가기
프로그램/Etc

[Log4j] Log4j 설정 Reference

by 로드러너 2014. 2. 11.
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

1. Priorities

     로깅 레벨은 다음과 같다.

     DEBUG > INFO > WARN < ERROR < FATAL

 
Level Description
ALL Output of all messages
TRACE Detailed Debugging
DEBUG Debugging (Method xy called with param abc …)
INFO General information (Program started/ended, connected to host foo, calculation took n seconds)
WARN Unexpected situation
ERROR Error (Exception caught)
FATAL Critical error, program stops
OFF Logging is deactivated

     Example

          log4j.rootLogger=Level, Appender, Appender, …

          log4j.logger.Package.Package.Class=Level

 

 

2. Appenders

 
Name Writes to
ConsoleAppender 콘솔에 출력하는 방식
FileAppender File
RollingFileAppender 일정 용량만큼 파일에 쓰는 방식. MaxFileSize로 파일의 최대크기를 정한다.MaxBackupIndex으로 파일최대갯수를 지정한다. 만약 최대갯수까지 차면 처음의 로그파일에 재기록한다.
DailyRollingFileAppender 시간을 주기로 파일을 생성하여 기록하는 방식. DatePattern으로 시간 단위를 설정할 수 있다.
SyslogAppender UNIX Syslog
NTEventLogAppender Windows Event-Log
SMTPAppender e-mail
net.SocketAppender Socket (client mode)
net.SocketHubAppender Socket (server mode)

     Example

          log4j.appender.Appender=org.apache.log4j.Name

……

     Example – DailyRollingFileAppender

          log4j.appender.logfile=org.apache.log4j.DailyRollingFileAppender

          #월단위 log4j.appender.logfile.DatePattern=’.'yyyy-MM

          #주단위 log4j.appender.logfile.DatePattern=’.'yyyy-MM-ww

          #12시간단위 log4j.appender.logfile.DatePattern=’.'yyyy-MM-dd-a

          #시간단위 log4j.appender.logfile.DatePattern=’.'yyyy-MM-dd-HH

          #분단위 log4j.appender.logfile.DatePattern=’.'yyyy-MM-dd-HH-mm

     Example – RollingFileAppender

          log4j.appender.logfile=org.apache.log4j.RollingFileAppender

          log4j.appender.logfile.MaxFileSize=512KB

          log4j.appender.logfile.MaxBackupIndex=3

 

 

3. Layouts

 
Name Description
PatternLayout 출력 패턴을 직정정의한다.
SimpleLayout 미리 정의된 단순한 패턴으로 출력한다.
TTCCLayout = time, thread, category, context . SimpleLayout보다는 많이 나옴
HTMLLayout HTML테이블 형식으로 만들어준다.
XMLLayout XML

     Example

          log4j.appender.Appender.layout=org.apache.log4j.Name

     Example – SimpleLayout

          log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout

     Example – HTMLLayout

          log4j.appender.stdout.layout=org.apache.log4j.HTMLLayout

     Example – TTCCLayout

          log4j.appender.stdout.layout=org.apache.log4j.TTCCLayout

 

 

4. PatternLayout

 
Level Description
c category; a.b.c -> %c{2}=a.b
C fully qualified class name (slow);
d date; %d{HH:mm:ss,SSS}, %d{ABSOLUTE}, %dd{DATE}, %d{ISO8601}
F file name (extremly slow)
l location (extremly slow)
L line number (extremly slow)
% percent sign
m message
M method name (extremly slow)
n line separator; \n or \r\n
p priority
r milliseconds from layout construction
t thread name
x nested diagnostic context of thread
X mapped diagnositc context of thred; %X{clientNumber}

 

5. Format Modifier

 
Modifier left justify min. width max. width comment
%20c false 20 none Left pad with spaces if the name is less than 20 characters long.
%-20c true 20 none Right pad with spaces if the name is less than 20 characters long.
%.30c NA none 30 Truncate from the beginning if the name is longer than 30 characters.
%20.30c false 20 30 Left pad with spaces if the name is shorter than 20 characters.
However, if category name is longer than 30 characters, then truncate
from the beginning.
%-20.30c true 20 30 Right pad with spaces if the name is shorter than 20 characters.
However, if category name is longer than 30 characters, then truncate
from the beginning.

 

6. 전체 예

     log4j.rootLogger=WARN, stdout, logfile, socket   

     log4j.appender.stdout=org.apache.log4j.ConsoleAppender
     log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
     log4j.appender.stdout.layout.ConversionPattern=%-5p [%-4t] %-25.25c %m%n

     log4j.appender.logfile=org.apache.log4j.RollingFileAppender
     log4j.appender.logfile.File=${java.io.tmpdir}/myapp.log
     log4j.appender.logfile.MaxFileSize=512KB
     log4j.appender.logfile.MaxBackupIndex=3
     log4j.appender.logfile.layout=org.apache.log4j.PatternLayout

     log4j.appender.logfile.layout.ConversionPattern=%d %r [%t] %-5p %c %x – %m%n
     log4j.appender.socket=org.apache.log4j.net.SocketHubAppender
     log4j.appender.socket.port=10005
     log4j.appender.socket.locationInfo=true

     log4j.logger.com.my.app=DEBUG