新学的log4j的东西,写了三个类,分别以不同的形式输出日志。

htmlandwrite

package org.wondersgroup.log;

import java.io.*;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.HTMLLayout;
import org.apache.log4j.WriterAppender;

/**
 * @author Administrator
 *
 * HTMLLayout和WriterAppender
 *
 */
public class htmlandwrite {
    static Logger logger = Logger.getLogger(htmlandwrite.class);

    public static void main(String args[]) {
        HTMLLayout layout = new HTMLLayout();
        WriterAppender appender = null;
        try {
            FileOutputStream output = new FileOutputStream(
                    "WebContent/WEB-INF/HTMLlog/output2.html");
            appender = new WriterAppender(layout, output);
        } catch (Exception e){}
        
        logger.addAppender(appender);
        logger.setLevel((Level) Level.DEBUG);
        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");
    }

}

simpandfile

 

package org.wondersgroup.log;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.SimpleLayout;
import org.apache.log4j.FileAppender;

/**
 * @author Administrator
 *
 * SimpleLayout和FileAppender
 */
public class simpandfile {
    
    static Logger logger = Logger.getLogger(simpandfile.class);

    public static void main(String args[]) {

        SimpleLayout layout = new SimpleLayout();
        FileAppender appender = null;

        try {
            appender = new FileAppender(layout, "WebContent/WEB-INF/log/output1.txt", false);
        } catch (Exception e) {}
        
        logger.addAppender(appender);
        logger.setLevel((Level) Level.DEBUG);
        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");
    }
}

 

consandpatt

package org.wondersgroup.log;


import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.ConsoleAppender;
public class consandpatt {
    
/**
 * PatternLayout和ConsoleAppender
 */
static Logger logger = Logger.getLogger(consandpatt.class);
   
   public static void main(String args[]) {
       
      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);
      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 2008-10-08 13:07  小星星☆★  阅读(412)  评论(0编辑  收藏  举报

导航