一个例子:
1。java class
package com.netcobol.log;import org.apache.log4j.*;/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */import org.apache.log4j.*;
import com.sinotrans.util.PropertyUtil;// How to use log4j
public class TestLogging {    // Initialize a logging category.  Here, we get THE ROOT CATEGORY
    //static Category cat = Category.getRoot();
    // Or, get a custom category
    static Category cat = Category.getInstance(TestLogging.class.getName());
    static {
      try{
        org.apache.log4j.PropertyConfigurator.configure(PropertyUtil.getProperty(
            "log4j.properties"));
      }catch(Exception e){      }
    }
    // From here on, log away!  Methods are: cat.debug(your_message_string),
    // cat.info(...), cat.warn(...), cat.error(...), cat.fatal(...)    public static void main(String args[]) {
        // Try a few logging methods
        cat.debug("Start of main()");
        cat.info("Just testing a log message with priority set to INFO");
        cat.warn("Just testing a log message with priority set to WARN");
        cat.error("Just testing a log message with priority set to ERROR");
        cat.fatal("Just testing a log message with priority set to FATAL");
        try {
          org.apache.log4j.PropertyConfigurator.configure(PropertyUtil.getProperty("xxx.properties"));        }
        catch (Exception ex) {
          ex.printStackTrace();
          cat.error("Failed to load xxx.proerties", ex);
        }
        // Alternate but INCONVENIENT form
        cat.log(Priority.DEBUG, "Calling init()");        new TestLogging().init();
    }    public void init() {
        java.util.Properties prop = System.getProperties();
        java.util.Enumeration enum = prop.propertyNames();        cat.info("***System Environment As Seen By Java***");
        cat.debug("***Format: PROPERTY = VALUE***");        while (enum.hasMoreElements()) {
            String key = (String) enum.nextElement();
            cat.info(key + " = " + System.getProperty(key));
        }
    }}2.
#### Use two appenders, one to log to console, another to log to a file
log4j.rootCategory=debug, stdout, R# Print only messages of priority WARN or higher for your category
log4j.category.your.category.name=WARN
# Specifically inherit the priority level
#log4j.category.your.category.name=INHERITED#### First appender writes to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n#### Second appender writes to a file
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=c:/example.log# Control the maximum log file size
log4j.appender.R.MaxFileSize=100KB
# Archive log files (one backup file here)
log4j.appender.R.MaxBackupIndex=1log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n

解决方案 »

  1.   

    package com.sinotrans.util;import java.util.Properties;
    import java.io.InputStream;
    import java.io.IOException;
    import org.apache.log4j.Category;public class PropertyUtil {
      private static Properties property;
      private String propertyFile;
      //static Category cat = Category.getInstance(PropertyUtil.class.getName());
      /*
           static {
        try {
          org.apache.log4j.PropertyConfigurator.configure(PropertyUtil.getProperty(
              "log4j.properties"));
        }
        catch (Exception e) {
          e.printStackTrace();
        }
           }*/  private PropertyUtil(String propertyFile) throws IOException {
        this.propertyFile = propertyFile;
        InputStream input = this.getClass().getClassLoader().getResourceAsStream(this.
            propertyFile);
        property = new Properties();
        property.load(input);
      }  public static Properties getProperty(String propertyFile) throws IOException {
        if (property == null) {
          new PropertyUtil(propertyFile);
        }
        return property;
      }  public void setPropertyFile(String propertyFile) {
        this.propertyFile = propertyFile;
      }  public String getPropertyFile() {
        return propertyFile;
      }  public static void main(String[] args) {
        System.out.println("dklsfjl;dsa");
        try {
          Properties pro = PropertyUtil.getProperty("log4j.properties");
          //System.out.println(pro.get("log4j.rootCategory"));
        }
        catch (Exception e) {
          e.printStackTrace();
        }  }}
      

  2.   

    几个参考网站:
    http://hedong.3322.org/archives/000193.html
    http://hedong.3322.org/archives/000195.html
    http://hedong.3322.org/archives/000197.html