log4j - 测试用例:Layout

 

package com.yt.test.log4j.layout;

import java.io.FileOutputStream;

import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.HTMLLayout;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.WriterAppender;

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

    public static void initHTMLLayout() {
        try {
            HTMLLayout layout = new HTMLLayout();
            FileOutputStream output = new FileOutputStream("output2.html");
            WriterAppender appender = new WriterAppender(layout, output);
            logger.addAppender(appender);
            logger.setLevel((Level) Level.DEBUG);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void initPatternLayout() { 
        try {
            String pattern =  "Milliseconds since program start: %r %n";
      pattern += "Classname of caller: %C %n";
      pattern += "Date in ISO8601 format: %d{ISO8601} %n";
      pattern += "Location of log event: %l %n";
      pattern += "Message: %m %n %n"; 
      PatternLayout layout = new PatternLayout(pattern);
      ConsoleAppender appender = new ConsoleAppender(layout);
      logger.addAppender(appender);
      logger.setLevel((Level) Level.DEBUG);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String args[]) {
        LayoutTest.initHTMLLayout();
        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");
    }
}

 

posted on 2017-08-08 20:33  疯狂的妞妞  阅读(113)  评论(0编辑  收藏  举报

导航