FileDocCategorySizeDatePackage
PropertyPrinter.javaAPI DocApache log4j 1.2.155088Sat Aug 25 00:09:42 BST 2007org.apache.log4j.config

PropertyPrinter

public class PropertyPrinter extends Object implements PropertyGetter$PropertyCallback
Prints the configuration of the log4j default hierarchy (which needs to be auto-initialized) as a propoperties file on a {@link PrintWriter}.
author
Anders Kristensen

Fields Summary
protected int
numAppenders
protected Hashtable
appenderNames
protected Hashtable
layoutNames
protected PrintWriter
out
protected boolean
doCapitalize
Constructors Summary
public PropertyPrinter(PrintWriter out)

  
  
    
    this(out, false);
  
public PropertyPrinter(PrintWriter out, boolean doCapitalize)

    this.out = out;
    this.doCapitalize = doCapitalize;
    
    print(out);
    out.flush();
  
Methods Summary
public static java.lang.Stringcapitalize(java.lang.String name)

    if (Character.isLowerCase(name.charAt(0))) {
      if (name.length() == 1 || Character.isLowerCase(name.charAt(1))) {
        StringBuffer newname = new StringBuffer(name);
        newname.setCharAt(0, Character.toUpperCase(name.charAt(0)));
        return newname.toString();
      }
    }
    return name;
  
public voidfoundProperty(java.lang.Object obj, java.lang.String prefix, java.lang.String name, java.lang.Object value)

    // XXX: Properties encode value.toString()
    if (obj instanceof Appender && "name".equals(name)) {
      return;
    }
    if (doCapitalize) {
      name = capitalize(name);
    }
    out.println(prefix + name + "=" + value.toString());
  
protected java.lang.StringgenAppName()

    return "A" + numAppenders++;
  
protected booleanisGenAppName(java.lang.String name)
Returns true if the specified appender name is considered to have been generated, that is, if it is of the form A[0-9]+.

    if (name.length() < 2 || name.charAt(0) != 'A") return false;
    
    for (int i = 0; i < name.length(); i++) {
      if (name.charAt(i) < '0" || name.charAt(i) > '9") return false;
    }
    return true;
  
public static voidmain(java.lang.String[] args)

    new PropertyPrinter(new PrintWriter(System.out));
  
public voidprint(java.io.PrintWriter out)
Prints the configuration of the default log4j hierarchy as a Java properties file on the specified Writer.

N.B. print() can be invoked only once!

    printOptions(out, Logger.getRootLogger());
    
    Enumeration cats = LogManager.getCurrentLoggers();
    while (cats.hasMoreElements()) {
      printOptions(out, (Logger) cats.nextElement());
    }
  
protected voidprintOptions(java.io.PrintWriter out, org.apache.log4j.Category cat)

    Enumeration appenders = cat.getAllAppenders();
    Level prio = cat.getLevel();
    String appenderString = (prio == null ? "" : prio.toString());
    
    while (appenders.hasMoreElements()) {
      Appender app = (Appender) appenders.nextElement();
      String name;
      
      if ((name = (String) appenderNames.get(app)) == null) {
      
        // first assign name to the appender
        if ((name = app.getName()) == null || isGenAppName(name)) {
            name = genAppName();
        }
        appenderNames.put(app, name);
        
        printOptions(out, app, "log4j.appender."+name);
        if (app.getLayout() != null) {
          printOptions(out, app.getLayout(), "log4j.appender."+name+".layout");
        }
      }
      appenderString += ", " + name;
    }
    String catKey = (cat == Logger.getRootLogger())
        ? "log4j.rootLogger"
        : "log4j.logger." + cat.getName();
    if (appenderString != "") {
      out.println(catKey + "=" + appenderString);
    }
    if (!cat.getAdditivity() && cat != Logger.getRootLogger()) {
    	out.println("log4j.additivity." + cat.getName() + "=false");    
    }
  
protected voidprintOptions(java.io.PrintWriter out, org.apache.log4j.Logger cat)

      printOptions(out, (Category) cat);
  
protected voidprintOptions(java.io.PrintWriter out, java.lang.Object obj, java.lang.String fullname)

    out.println(fullname + "=" + obj.getClass().getName());
    PropertyGetter.getProperties(obj, this, fullname + ".");