1. log4j 패키지 다운로드 ( http://logging.apache.org/ )
2. 패키지 추가
3. properties.xml 작성
4. 샘플 프로그램 작성
5. 실행 java -classpath .;%PATH% XMLPropertiesFileLoggerTest properties.xml
* properites.xml 작성시.. *
여러가지 appender를 추가하고, (javadoc참조)
<root> </root> 안에 사용할 appender를 추가하면 된다.
---------------------------------------------------------------
샘플 프로그램
---------------------------------------------------------------
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;
import java.io.IOException;
public class XMLPropertiesFileLoggerTest {
public static void main(String args[]) {
String resource = args[0];
DOMConfigurator.configure(resource);
Logger logger = Logger.getLogger(XMLPropertiesFileLoggerTest.class);
logger.debug("logger debug");
logger.info("logger info");
logger.warn("logger warning");
logger.error("logger error");
logger.error("logger error" , new IOException("IOException!!!!!!"));
logger.fatal("logger fatal");
logger.fatal("logger fatal" , new SecurityException("Fatal Exception!!!!"));
}
}
properties.xml 샘플
-------------------------------------------------------------------------
<?xml version="1.0" encoding="euc-kr"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!-- 콘솔로 보기(디버그) -->
<appender name="CONSOLE-debug" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%p - %C{1} : %M %m %n"/>
</layout>
</appender>
<!-- 콘솔로 보기(인포) -->
<appender name="CONSOLE-info" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%m ---%d %n"/>
</layout>
</appender>
<!-- 매 날짜별 로그 파일 남기기 -->
<appender name="LOGFILE-DAILY" class="org.apache.log4j.DailyRollingFileAppender">
<param name="file" value="test-daily.log"/>
<param name="Append" value="true"/>
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%t] %-17c{2} (%13F:%L) %3x - %m%n"/>
</layout>
</appender>
<!-- 매 날짜별 로그 파일 남기기(로그인) -->
<appender name="LOGFILE-DAILY-LOGIN" class="org.apache.log4j.DailyRollingFileAppender">
<param name="file" value="test-daily-login.log"/>
<param name="Append" value="true"/>
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d - %m%n"/>
</layout>
</appender>
<!-- 매 날짜별 로그 파일 남기기(가입) -->
<appender name="LOGFILE-DAILY-JOIN" class="org.apache.log4j.DailyRollingFileAppender">
<param name="file" value="test-daily-join.log"/>
<param name="Append" value="true"/>
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d - %m%n"/>
</layout>
</appender>
<!-- 매 날짜별 로그 파일 남기기(탈퇴) -->
<appender name="LOGFILE-DAILY-JOINBREAK" class="org.apache.log4j.DailyRollingFileAppender">
<param name="file" value="test-daily-joinbreak.log"/>
<param name="Append" value="true"/>
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d - %m%n"/>
</layout>
</appender>
<!-- 로그 파일 100KB마다 새로 남기기 -->
<appender name="LOGFILE-SIZE" class="org.apache.log4j.RollingFileAppender">
<param name="file" value="test-Size.log"/>
<param name="Append" value="true"/>
<param name="MaxFileSize" value="100KB"/>
<param name="MaxBackupIndex" value="5"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%t] %-17c{2} (%13F:%L) %3x - %m%n"/>
</layout>
</appender>
<!-- XML 형태로 로그 파일 남기기 -->
<appender name="LOGFILE-XML" class="org.apache.log4j.FileAppender">
<param name="file" value="test-XML.log"/>
<param name="Append" value="true"/>
<layout class="org.apache.log4j.xml.XMLLayout"/>
</appender>
<logger name="org.apache">
<level value="WARN"/>
</logger>
<root>
<level value="DEBUG"/>
<appender-ref ref="CONSOLE-debug"/>
<!--<appender-ref ref="LOGFILE-DAILY"/>-->
<!--<appender-ref ref="LOGFILE-SIZE"/>-->
<!--<appender-ref ref="CONSOLE-info"/>-->
<appender-ref ref="LOGFILE-XML"/>
</root>
</log4j:configuration>
---------------------------------------------------------------------------------
>>> 실행 결과 <<<
콘솔
----------------------------------------------------------------------------------
DEBUG - XMLPropertiesFileLoggerTest : main logger debug
INFO - XMLPropertiesFileLoggerTest : main logger info
WARN - XMLPropertiesFileLoggerTest : main logger warning
ERROR - XMLPropertiesFileLoggerTest : main logger error
ERROR - XMLPropertiesFileLoggerTest : main logger error
java.io.IOException: IOException!!!!!!
at XMLPropertiesFileLoggerTest.main(XMLPropertiesFileLoggerTest.java:23)
FATAL - XMLPropertiesFileLoggerTest : main logger fatal
FATAL - XMLPropertiesFileLoggerTest : main logger fatal
java.lang.SecurityException: Fatal Exception!!!!
at XMLPropertiesFileLoggerTest.main(XMLPropertiesFileLoggerTest.java:26)
----------------------------------------------------------------------------------
test-XML.log 파일
----------------------------------------------------------------------------------
<log4j:event logger="XMLPropertiesFileLoggerTest" timestamp="1196037902593" level="DEBUG" thread="main">
<log4j:message><![CDATA[logger debug]]></log4j:message>
</log4j:event>
<log4j:event logger="XMLPropertiesFileLoggerTest" timestamp="1196037902593" level="INFO" thread="main">
<log4j:message><![CDATA[logger info]]></log4j:message>
</log4j:event>
<log4j:event logger="XMLPropertiesFileLoggerTest" timestamp="1196037902593" level="WARN" thread="main">
<log4j:message><![CDATA[logger warning]]></log4j:message>
</log4j:event>
<log4j:event logger="XMLPropertiesFileLoggerTest" timestamp="1196037902593" level="ERROR" thread="main">
<log4j:message><![CDATA[logger error]]></log4j:message>
</log4j:event>
<log4j:event logger="XMLPropertiesFileLoggerTest" timestamp="1196037902593" level="ERROR" thread="main">
<log4j:message><![CDATA[logger error]]></log4j:message>
<log4j:throwable><![CDATA[java.io.IOException: IOException!!!!!!
at XMLPropertiesFileLoggerTest.main(XMLPropertiesFileLoggerTest.java:23)
]]></log4j:throwable>
</log4j:event>
<log4j:event logger="XMLPropertiesFileLoggerTest" timestamp="1196037902593" level="FATAL" thread="main">
<log4j:message><![CDATA[logger fatal]]></log4j:message>
</log4j:event>
<log4j:event logger="XMLPropertiesFileLoggerTest" timestamp="1196037902593" level="FATAL" thread="main">
<log4j:message><![CDATA[logger fatal]]></log4j:message>
<log4j:throwable><![CDATA[java.lang.SecurityException: Fatal Exception!!!!
at XMLPropertiesFileLoggerTest.main(XMLPropertiesFileLoggerTest.java:26)
]]></log4j:throwable>
</log4j:event>
----------------------------------------------------------------------------------