log4j的使用

import java.io.FileOutputStream;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.SimpleLayout;
import org.apache.log4j.FileAppender;
import org.apache.log4j.HTMLLayout;
import org.apache.log4j.WriterAppender;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.ConsoleAppender;


public class Log4jTest {
    private static Logger logger= Logger.getLogger( Log4jTest.class );
    
    /**
     * Simpler Layout and File Appender test.
     */
    private void doSimpleFileTest(){        
        SimpleLayout layout = new SimpleLayout();
        FileAppender appender = null;
        try {
            appender = new FileAppender(layout,"output1.txt",false);
        } catch (Exception ex) {}        
        
        logger.addAppender( appender );
        logger.setLevel( Level.DEBUG );

        // test log
        testLog();

    }// end doSimpleFileTest method
    
    /**
     * HTML layout and writer appender test.
     */
    private void doHTMLWriterTest(){
        HTMLLayout layout = new HTMLLayout();
        layout.setTitle( "Log4j test: html format" );

        WriterAppender appender = new WriterAppender();
        try {
            FileOutputStream out = new FileOutputStream("output2.html",false);
            appender = new WriterAppender(layout, out);
         } catch (Exception ex) {}
         
         logger.addAppender( appender );
         logger.setLevel( Level.DEBUG );
         
         // test log
         testLog();
    }
    
    /**
     * Pattern layout and console appender test.
     */
    private void doPatternConsoleTest(){
        // pattern
        String pattern = "Milliseconds since program start: %r %n" +
            "Classname of caller: %C %n" +
            "Data in ISO8601 format: %d{ISO8601} %n" +
            "Data in DATE format: %d{DATE} %n" +
            "Data in ABSOLUTE format: %d{ABSOLUTE} %n" +
            "Location of log event: %l %n" +
            "Message: %m %n %n";
        PatternLayout layout = new PatternLayout( pattern );
        ConsoleAppender appender = new ConsoleAppender( layout );
        
        logger.addAppender( appender );
        logger.setLevel( Level.DEBUG );
        
        // test
        testLog();
    }
    
    
    // helper method testLog
    private void testLog(){
        logger.debug( "Here is some DEBUG" );
        logger.info( "Here is some INFO" );
        logger.warn( "Here is some WARN" );
        logger.error( "Here is some ERROR" );
        logger.fatal( "Here is some FATAL" );
        logger.info( "中文日志");
    }
    
    public static void main( String[] args ){
        Log4jTest test = new Log4jTest();
        test.doSimpleFileTest();
        test.doHTMLWriterTest();
        test.doPatternConsoleTest();
    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值