<font face='±¼¸²'>À̹ø±ÛÀº XMLÀ» ÀÌ¿ëÇÑ property¼³Á¤À» ÀÌ¿ëÇÏ¿© jakarta-log4jÀÇ È¯°æ¼³Á¤ ¹×
Å×½ºÆ® ±¸µ¿ÆÄÀÏ¿¡ ´ëÇÑ »ç¿ë¹æ¹ýÀ» ¼³¸íÇÑ´Ù.
±âº»ÀûÀ¸·Î log4j¸¦ ´Ù¿î·Îµå ¹Þ¾Æ¾ß Çϸç, ÀÌ ÆÐÅ°Áö´Â ÆÛÆ÷¸Õ½ºÃÖÀûÈ,
¼öÇàÁßÀÇ ´Ù¸¥ ÇÁ·Î±×·¥°úÀÇ Ãæµ¹µî¿¡¼ ¿©·¯ °¡Áö »çÇ×À» °í·ÁÇÑ ÆÐÅ°ÁöÀ̸ç,
°¡Àå °£´ÜÇÏ°Ô ·Î±×¸¦ ±â·ÏÇÏ°í º¼¼ö ÀÖ´Â ÁÁÀº ÆÐÅ°ÁöÀÌ´Ù..
¿ì¼± log4j¸¦ ´Ù¿î¹Þ¾Æ Ŭ·¡½ºÆнº¿¡ Ãß°¡ÇÏ¿© ¾ÖÇø®ÄÉÀÌ¼Ç ÇüÅ·Πµµ´Â ÇÁ·Î±×·¥À»
Å×½ºÆ®Çغ¸µµ·Ï ÇÑ´Ù.
1. Log4Jȯ°æ¼³Á¤¹æ¹ý
- jakarta.apache.org¿¡¼ log4jÀÇ ÃÖ½ÅÆÇ(binary)À» ´Ù¿î·Îµå ¹Þ´Â´Ù.
- ¾ÐÃàÀ» ǬÈÄ log4j libraryÆÄÀÏÀ» Ŭ·¡½º Æнº¿¡ Ãß°¡ÇÑ´Ù.
2. Log4JÀÇ µÎ°¡Áö ÇÁ·ÎÆÛƼ ÇüÅÂ
Log4J´Â ÀϹÝÀûÀ¸·Î XMLÇüÅÂÀÇ ¼³Á¤À» °¡Áø property¿Í propertiesÇüÅ·ΠÀоîµéÀÌ´Â
µÎ°¡ÁöÀÇ Á¾·ù°¡ ÀÖ´Ù. º¸Åë °³¹ßÀ» ÇÒ °æ¿ì propertyÆÄÀÏÀ» ÀÌ¿ëÇÏ´Â °æ¿ì´Â ¸¹À¸³ª
xml¿¡ ´ëÇÑ ¼³Á¤¹æ¹ý ¹× ȯ°æ¿¡ ´ëÇÑ ÇѱÛÂüÁ¶°¡ ¾ø¾î¼ ¿©±â¼´Â xmlÀ» ÀÌ¿ëÇÑ
DOMConfigurator¸¦ »ç¿ëÇÏ¿© »ùÇÃÅ×½ºÆ®¸¦ Çغ¸µµ·Ï ÇÏ°Ú´Ù.
3. »ùÇÃÇÁ·Î±×·¥
¿ì¼± ³»°¡ ¿øÇÏ´Â loggerŬ·¡½º¸¦ »ç¿ëÇÒ ¼ö Àִ Ŭ·¡½º¸¦ <b>ÆÑÅ丮ÆÐÅÏ</b>À» ±¸»çÇÏ¿©
¸¸µé¾îº¸µµ·Ï ÇÏ°Ú´Ù.
ÀÌÆÄÀÏÀº LogUtilÀ̶õ Ŭ·¡½º¸¦ »ç¿ëÇÏ¿´À¸¸ç º»»çÀÌÆ®¿¡µµ Àû¿ëµÇ¾î ÀÖ´Ù.
/*
* @(#)LogUtil.java 1.00 2003.02.01
*
* Copyright Choi Ji Woong, All Rights Reserved.
*
*
*/
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;
import java.io.IOException;
import java.net.URL;
/**
* XML Property logger testÀ̳׿ä..È÷È÷
* <p><p>
*
* @author ³î»õ(ienvyou@jlook.com)
* @version 1.00, 2003.02.01
* @since log4j-1.2.7
*/
public class LogUtil {
//private static Logger logger = LogUtil.getLogger(OtherClass.class);
public static void init(String xmlFile) {
String resource = xmlFile;
URL configFileResource = LogUtil.class.getResource(resource);
// System.out.println("URL =======> " + configFileResource +
" ::::: " + configFileResource.getFile());
//DOMConfigurator.configure(configFileResource.getFile());
DOMConfigurator.configure(xmlFile);
}
public static Logger getLogger(Class c) {
return Logger.getLogger(c);
}
}
´ÙÀ½Àº ÇØ´ç Ŭ·¡½º¸¦ Å×½ºÆ®Çغ¼¼ö ÀÖ´Â ¼¼°³ÀÇ Å¬·¡½º¸¦ ³ª¿Çغ¸µµ·Ï ÇÏ°Ú´Ù.
/*
* @(#)OtherClass.java 1.00 2003.02.01
*
* Copyright Choi Ji Woong, All Rights Reserved.
*
*
*/
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;
import java.io.IOException;
import java.net.URL;
/**
* XML Property logger testÀ̳׿ä..È÷È÷
* <p><p>
*
* @author ³î»õ(ienvyou@jlook.com)
* @version 1.00, 2003.02.01
* @since log4j-1.2.7
*/
public class OtherClass {
public OtherClass() {
Logger logger = LogUtil.getLogger(OtherClass.class);
logger.debug("Other Class debug");
logger.info("Other Class info");
logger.warn("Other Class warn");
logger.fatal("Other Class fatal");
logger.error("Other Class error");
}
}
/*
* @(#)XMLPropertiesFileLoggerTest.java 1.00 2003.02.01
*
* Copyright Choi Ji Woong, All Rights Reserved.
*
*
*/
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;
import java.io.IOException;
import java.net.URL;
/**
* XML Property logger testÀ̳׿ä..È÷È÷
* <p><p>
*
* @author ³î»õ(ienvyou@jlook.com)
* @version 1.00, 2003.02.01
* @since log4j-1.2.7
*/
public class XMLPropertiesFileLoggerTest {
public static void main(String argv[]) {
LogUtil.init(argv[0]);
Logger logger = LogUtil.getLogger(XMLPropertiesFileLoggerTest.class);
// Add a bunch of logging statements ...
logger.debug("Hello,³î»õ1~");
logger.debug("Hello,³î»õ2~");
logger.debug("Hello,³î»õ3~");
logger.debug("Hello,³î»õ4~");
logger.debug("Hello,³î»õ5~");
logger.info("³î»õÁ¤º¸1");
logger.info("³î»õÁ¤º¸2");
logger.info("³î»õÁ¤º¸3");
logger.info("³î»õÁ¤º¸4");
logger.info("³î»õÁ¤º¸5");
logger.warn("¾Æ¶Ù~ ¿ö´×À̾ß~1");
logger.warn("¾Æ¶Ù~ ¿ö´×À̾ß~2");
logger.warn("¾Æ¶Ù~ ¿ö´×À̾ß~3");
logger.warn("¾Æ¶Ù~ ¿ö´×À̾ß~4");
logger.warn("¾Æ¶Ù~ ¿ö´×À̾ß~5");
logger.error("Çä¶Ñ~~ ¿¡·¯¾ß1");
logger.error("Çä¶Ñ~~ ¿¡·¯¾ß2" , new IOException("¾Æ~ ¶Ù¹Ù~"));
logger.error("Çä¶Ñ~~ ¿¡·¯¾ß3");
logger.error("Çä¶Ñ~~ ¿¡·¯¾ß4" , new IllegalStateException("Error !!"));
logger.fatal("¾ù..Ä¡¸íŸ´Ù1");
logger.fatal("¾ù..Ä¡¸íŸ´Ù2" , new SecurityException("Fatal Exception"));
logger.fatal("¾ù..Ä¡¸íŸ´Ù3");
logger.fatal("¾ù..Ä¡¸íŸ´Ù4" , new SecurityException("Fatal Exception"));
new OtherClass();
}
}
À§¿Í °°ÀÌ ÀÛ¼ºÀÌ µÉ ¼ö ÀÖÀ¸¸ç ½ÇÇà¹æ¹ýÀº
ÄÄÆÄÀÏ
javac -classpath %LOG4J_HOME%\lib\log4j-1.2.7.jar; XMLPropertiesFileLoggerTest.java
½ÇÇà
java -classpath %LOG4J_HOME%\lib\log4j-1.2.7.jar; XMLPropertiesFileLoggerTest config.xml
´ÙÀ½ÆäÀÌÁö¿¡¼´Â ¿©·¯°¡Áö ȯ°æ¼³Á¤À» ÇÒ ¼ö ÀÖ´Â xmlÆÄÀÏÀ» ¼Ò°³ÇÑ´Ù.
|