2012年9月19日 星期三

log4j


Writing step:

a. write a log4j.properties in your package
{code}
log4j.rootLogger=Info, A1


# A1 is set to be a ConsoleAppender
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
# %d is date and it's format
# %p is output the log priority info DEBUG INFO WARN ERROR FATAL
# %c is class name
# %l is line position
# %m is the message in contents
# %n is new line
# output format
#[12/09/20 09:26:10][DEBUG][HelloWorld-14] Sample debug message
#[12/09/20 09:26:10][INFO][HelloWorld-15] Sample info message
#[12/09/20 09:26:10][WARN][HelloWorld-16] Sample warn message
log4j.appender.A1.layout.ConversionPattern=[%d{yy/MM/dd HH:mm:ss}][%p][%C-%L] %m%n
{code}

b. write a class call hello word
{code}
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;


public class HelloWorld {
   
    static final Logger logger = Logger.getLogger(HelloWorld.class);

    // Reference http://werdna1222coldcodes.blogspot.tw/2009/11/javalog4j.html
    public static void main(String[] args) {
            // 會自動設定
            //BasicConfigurator.configure();
            System.out.println("Start Log4j test");
            logger.debug("Sample debug message");
            logger.info("Sample info message");
            logger.warn("Sample warn message");
            logger.error("Sample error message");
            logger.fatal("Sample fatal message");
    }
    
}

{code}

c. Reference
http://werdna1222coldcodes.blogspot.tw/2009/11/javalog4j.html

沒有留言:

張貼留言