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    /**
94     * @since 1.2.15
95     */
96    protected
97    void printOptions(PrintWriter out, Category cat) {
98      Enumeration appenders = cat.getAllAppenders();
99      Level prio = cat.getLevel();
100     String appenderString = (prio == null ? "" : prio.toString());
101     
102     while (appenders.hasMoreElements()) {
103       Appender app = (Appender) appenders.nextElement();
104       String name;
105       
106       if ((name = (String) appenderNames.get(app)) == null) {
107       
108         // first assign name to the appender
109         if ((name = app.getName()) == null || isGenAppName(name)) {
110             name = genAppName();
111         }
112         appenderNames.put(app, name);
113         
114         printOptions(out, app, "log4j.appender."+name);
115         if (app.getLayout() != null) {
116           printOptions(out, app.getLayout(), "log4j.appender."+name+".layout");
117         }
118       }
119       appenderString += ", " + name;
120     }
121     String catKey = (cat == Logger.getRootLogger())
122         ? "log4j.rootLogger"
123         : "log4j.logger." + cat.getName();
124     if (appenderString != "") {
125       out.println(catKey + "=" + appenderString);
126     }
127     if (!cat.getAdditivity() && cat != Logger.getRootLogger()) {
128     	out.println("log4j.additivity." + cat.getName() + "=false");    
129     }
130   }
131 
132   protected void printOptions(PrintWriter out, Logger cat) {
133       printOptions(out, (Category) cat);
134   }
135   
136   protected
137   void printOptions(PrintWriter out, Object obj, String fullname) {
138     out.println(fullname + "=" + obj.getClass().getName());
139     PropertyGetter.getProperties(obj, this, fullname + ".");
140   }
141   
142   public void foundProperty(Object obj, String prefix, String name, Object value) {
143     // XXX: Properties encode value.toString()
144     if (obj instanceof Appender && "name".equals(name)) {
145       return;
146     }
147     if (doCapitalize) {
148       name = capitalize(name);
149     }
150     out.println(prefix + name + "=" + value.toString());
151   }
152   
153   public static String capitalize(String name) {
154     if (Character.isLowerCase(name.charAt(0))) {
155       if (name.length() == 1 || Character.isLowerCase(name.charAt(1))) {
156         StringBuffer newname = new StringBuffer(name);
157         newname.setCharAt(0, Character.toUpperCase(name.charAt(0)));
158         return newname.toString();
159       }
160     }
161     return name;
162   }
163   
164   // for testing
165   public static void main(String[] args) {
166     new PropertyPrinter(new PrintWriter(System.out));
167   }
168 }