############################################################
# Default Logging Configuration File
#
# You can use a different file by specifying a filename
# with the java.util.logging.config.file system property.
# For example java -Djava.util.logging.config.file=myfile
############################################################
############################################################
# Global properties
############################################################
# "handlers" specifies a comma separated list of log Handler
# classes. These handlers will be installed during VM startup.
# Note that these classes must be on the system classpath.
# By default we only configure a ConsoleHandler, which will only
# show messages at the INFO and above levels.
handlers= java.util.logging.ConsoleHandler
# To also add the FileHandler, use the following line instead.
#handlers= java.util.logging.FileHandler, java.util.logging.ConsoleHandler
# Default global logging level.
# This specifies which kinds of events are logged across
# all loggers. For any given facility this global level
# can be overriden by a facility specific level
# Note that the ConsoleHandler also has a separate level
# setting to limit messages printed to the console.
.level= INFO
############################################################
# Handler specific properties.
# Describes specific configuration info for Handlers.
############################################################
# default file output is in user's home directory.
java.util.logging.FileHandler.pattern = %h/java%u.log
java.util.logging.FileHandler.limit = 50000
java.util.logging.FileHandler.count = 1
java.util.logging.FileHandler.formatter = java.util.logging.XMLFormatter
# Limit the message that are printed on the console to INFO and above.
java.util.logging.ConsoleHandler.level = INFO
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
############################################################
# Facility specific properties.
# Provides extra control for each logger.
############################################################
# For example, set the com.xyz.foo logger to only log SEVERE
# messages:
com.xyz.foo.level = SEVERE
logging.properties 파일의 내용이다. 천천히 살펴보자.
# Default Logging Configuration File
#
# You can use a different file by specifying a filename
# with the java.util.logging.config.file system property.
# For example java -Djava.util.logging.config.file=myfile
디폴트 로깅 설정 파일이라는 것이고, -D 옵션을 넣어서 실행하여, java.util.logging.config.file 프로퍼티를 키로 설정 파일을 다른 것으로 지정할 수 있다는 것이다.
# Global properties
# "handlers" specifies a comma separated list of log Handler
# classes. These handlers will be installed during VM startup.
# Note that these classes must be on the system classpath.
# By default we only configure a ConsoleHandler, which will only
# show messages at the INFO and above levels.
handlers= java.util.logging.ConsoleHandler
# To also add the FileHandler, use the following line instead.
#handlers= java.util.logging.FileHandler, java.util.logging.ConsoleHandler
전역 설정. 즉, 로깅 전체에 영향을 미치는 설정을 나타낸다. handlers 프로퍼티는 로그 Handler 클래스 설정에 사용되는데, 로그 핸들러(log handler)가 여러 개인 경우에는 콤마로 구분하여 나열한다. 로그 핸들러 클래스는 당연히 클래스패스에 위치해야 하고, 디폴트는 INFO 이상의 로그 수위만 화면에 출력하는 ConsoleHandler이다.
# Default global logging level.
# This specifies which kinds of events are logged across
# all loggers. For any given facility this global level
# can be overriden by a facility specific level
# Note that the ConsoleHandler also has a separate level
# setting to limit messages printed to the console.
.level= INFO
모든 로거에 기본적으로 적용할 로그 수위는 INFO라는 것이다.
############################################################
# Handler specific properties.
# Describes specific configuration info for Handlers.
############################################################
# default file output is in user's home directory.
java.util.logging.FileHandler.pattern = %h/java%u.log
java.util.logging.FileHandler.limit = 50000
java.util.logging.FileHandler.count = 1
java.util.logging.FileHandler.formatter = java.util.logging.XMLFormatter
# Limit the message that are printed on the console to INFO and above.
java.util.logging.ConsoleHandler.level = INFO
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
특정 핸들러에만 적용시킬 프로퍼티. 자세한 내용은 파일 로거를 쓰면서 확인해보자.
############################################################
# Facility specific properties.
# Provides extra control for each logger.
############################################################
# For example, set the com.xyz.foo logger to only log SEVERE
# messages:
com.xyz.foo.level = SEVERE
특정 퍼시리티에만 적용시킬 프로퍼티, 핸드러별 설정보다 더 미세하게 특정 로거단위로도 설정이 가능하다.
# show messages at the INFO and above levels.
handlers= java.util.logging.ConsoleHandler
# To also add the FileHandler, use the following line instead.
#handlers= java.util.logging.FileHandler, java.util.logging.ConsoleHandler
위와 같은 설정을 아래처럼 바꿔서 파일 로그 핸들러도 쓰이도록 해보자.
# show messages at the INFO and above levels.
# handlers= java.util.logging.ConsoleHandler
# To also add the FileHandler, use the following line instead.
handlers= java.util.logging.FileHandler, java.util.logging.ConsoleHandler
어디에 출력이 되었을까? 힌트는 logging.properties 파일의 파일 핸들러 관련 설정이다.
# default file output is in user's home directory.
java.util.logging.FileHandler.pattern = %h/java%u.log
java.util.logging.FileHandler.limit = 50000
java.util.logging.FileHandler.count = 1
java.util.logging.FileHandler.formatter = java.util.logging.XMLFormatter
%h는 뭔가? JDK FileHandler API 문서에서 설명을 찾을 수 있다.
"/" the local pathname separator
"%t" the system temporary directory
"%h" the value of the "user.home" system property
"%g" the generation number to distinguish rotated logs
"%u" a unique number to resolve conflicts
"%%" translates to a single percent sign "%"
%h는 Home directory를 의미하는 것이다. 운영체제 그리고 윈도우의 경우는 버전에 따라 디렉토리가 조금씩 다르다. 기본적으로 NT 기반인 경우(필자는 XP Pro), "C:Documents and Settings로그인한 사용자 이름"이다. 홈 디렉토리 밑에 java로 시작하고, %u에 따라 파일이름에 의한 충돌을 방지하기 위한 일련번호를 붙힌다. 확장자는 log.
그래서, 필자의 경우는 홈 디렉토리에 java0.log라는 파일이 생겼다. 다른 위치에 파일이 출력되도록 변경해보자.
java.util.logging.FileHandler.pattern = mylog_%u.log
위와 같이 변경한 후 로그가 출력되는 프로그램을 실행시키자. 클래스 파일을 실행시킨 위치에 mylog0.log라는 이름으로 파일이 생길 것이다. 나머지 설정들의 의미를 알아보자.
java.util.logging.FileHandler.limit = 50000
하나의 파일에 최대로 기록될 바이트수를 대략적으로 지정한 것이다. 디폴트는 0이며, 무제한이다.
java.util.logging.FileHandler.count = 1
출력파일을 몇 개 사용할 것인가. 디폴트는 1.
java.util.logging.FileHandler.formatter = java.util.logging.XMLFormatter
출력 포맷을 결정하는 포맷터 클래스. 기본적으로 SimpleFormatter와 XMLFormatter가 있으며, 디폴트는 XMLFormatter이다.
SimpleFormatter를 사용한 경우의 출력과 XMLFormatter를 사용한 경우의 출력은 아래와 같다.
SimpleFormatter 사용시
2004. 11. 8 오후 4:49:34 CommonsLoggerExample main
정보: hello, log
XMLFormatter 사용시
<?xml version="1.0" encoding="x-windows-949" standalone="no"?>
<!DOCTYPE log SYSTEM "logger.dtd">
<log>
<record>
<date>2004-11-08T16:37:25</date>
<millis>1099899445360</millis>
<sequence>0</sequence>
<logger>CommonsLoggerExample</logger>
<level>INFO</level>
<class>CommonsLoggerExample</class>
<method>main</method>
<thread>10</thread>
<message>hello, log</message>
</record>
</log>