View Javadoc

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17
    18  package org.apache.log4j.config;
    19
    20  import org.apache.log4j.Appender;
    21  import org.apache.log4j.Category;
    22  import org.apache.log4j.Level;
    23  import org.apache.log4j.LogManager;
    24  import org.apache.log4j.Logger;
    25
    26  import java.io.PrintWriter;
    27  import java.util.Enumeration;
    28  import java.util.Hashtable;
    29
    30  /***
    31     Prints the configuration of the log4j default hierarchy
    32     (which needs to be auto-initialized) as a propoperties file
    33     on a {@link PrintWriter}.
    34
    35     @author  Anders Kristensen
    36   */
    37  public class PropertyPrinter implements PropertyGetter.PropertyCallback {
    38    protected int numAppenders = 0;
    39    protected Hashtable appenderNames = new Hashtable();
    40    protected Hashtable layoutNames   = new Hashtable();
    41    protected PrintWriter out;
    42    protected boolean doCapitalize;
    43
    44    public
    45    PropertyPrinter(PrintWriter out) {
    46      this(out, false);
    47    }
    48
    49    public
    50    PropertyPrinter(PrintWriter out, boolean doCapitalize) {
    51      this.out = out;
    52      this.doCapitalize = doCapitalize;
    53
    54      print(out);
    55      out.flush();
    56    }
    57
    58    protected
    59    String genAppName() {
    60      return "A" + numAppenders++;
    61    }
    62
    63    /***
    64     * Returns true if the specified appender name is considered to have
    65     * been generated, that is, if it is of the form A[0-9]+.
    66    */
    67    protected
    68    boolean isGenAppName(String name) {
    69      if (name.length() < 2 || name.charAt(0) != 'A') return false;
    70
    71      for (int i = 0; i < name.length(); i++) {
    72        if (name.charAt(i) < '0' || name.charAt(i) > '9') return false;
    73      }
    74      return true;
    75    }
    76
    77    /***
    78     * Prints the configuration of the default log4j hierarchy as a Java
    79     * properties file on the specified Writer.
    80     *
    81     * <p>N.B. print() can be invoked only once!
    82     */
    83    public
    84    void print(PrintWriter out) {
    85      printOptions(out, Logger.getRootLogger());
    86
    87      Enumeration cats = LogManager.getCurrentLoggers();
    88      while (cats.hasMoreElements()) {
    89        printOptions(out, (Logger) cats.nextElement());
    90      }
    91    }
    92
    93    protected
    94    void printOptions(PrintWriter out, Category cat) {
    95      Enumeration appenders = cat.getAllAppenders();
    96      Level prio = cat.getLevel();
    97      String appenderString = (prio == null ? "" : prio.toString());
    98
    99      while (appenders.hasMoreElements()) {
    100       Appender app = (Appender) appenders.nextElement();
    101       String name;
    102
    103       if ((name = (String) appenderNames.get(app)) == null) {
    104
    105         // first assign name to the appender
    106         if ((name = app.getName()) == null || isGenAppName(name)) {
    107             name = genAppName();
    108         }
    109         appenderNames.put(app, name);
    110
    111         printOptions(out, app, "log4j.appender."+name);
    112         if (app.getLayout() != null) {
    113           printOptions(out, app.getLayout(), "log4j.appender."+name+".layout");
    114         }
    115       }
    116       appenderString += ", " + name;
    117     }
    118     String catKey = (cat == Logger.getRootLogger())
    119         ? "log4j.rootLogger"
    120         : "log4j.logger." + cat.getName();
    121     if (appenderString != "") {
    122       out.println(catKey + "=" + appenderString);
    123     }
    124     if (!cat.getAdditivity() && cat != Logger.getRootLogger()) {
    125     	out.println("log4j.additivity." + cat.getName() + "=false");
    126     }
    127   }
    128
    129   protected void printOptions(PrintWriter out, Logger cat) {
    130       printOptions(out, (Category) cat);
    131   }
    132
    133   protected
    134   void printOptions(PrintWriter out, Object obj, String fullname) {
    135     out.println(fullname + "=" + obj.getClass().getName());
    136     PropertyGetter.getProperties(obj, this, fullname + ".");
    137   }
    138
    139   public void foundProperty(Object obj, String prefix, String name, Object value) {
    140     // XXX: Properties encode value.toString()
    141     if (obj instanceof Appender && "name".equals(name)) {
    142       return;
    143     }
    144     if (doCapitalize) {
    145       name = capitalize(name);
    146     }
    147     out.println(prefix + name + "=" + value.toString());
    148   }
    149
    150   public static String capitalize(String name) {
    151     if (Character.isLowerCase(name.charAt(0))) {
    152       if (name.length() == 1 || Character.isLowerCase(name.charAt(1))) {
    153         StringBuffer newname = new StringBuffer(name);
    154         newname.setCharAt(0, Character.toUpperCase(name.charAt(0)));
    155         return newname.toString();
    156       }
    157     }
    158     return name;
    159   }
    160
    161   // for testing
    162   public static void main(String[] args) {
    163     new PropertyPrinter(new PrintWriter(System.out));
    164   }
    165 }