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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.apache.log4j.config;
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;
26 import java.io.PrintWriter;
27 import java.util.Enumeration;
28 import java.util.Hashtable;
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}.
35 @author Anders Kristensen
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;
45 PropertyPrinter(PrintWriter out) {
50 PropertyPrinter(PrintWriter out, boolean doCapitalize) {
52 this.doCapitalize = doCapitalize;
60 return "A" + numAppenders++;
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]+.
68 boolean isGenAppName(String name) {
69 if (name.length() < 2 || name.charAt(0) != 'A') {
73 for (int i = 0; i < name.length(); i++) {
74 if (name.charAt(i) < '0' || name.charAt(i) > '9') {
82 * Prints the configuration of the default log4j hierarchy as a Java
83 * properties file on the specified Writer.
85 * <p>N.B. print() can be invoked only once!
88 void print(PrintWriter out) {
89 printOptions(out, Logger.getRootLogger());
91 Enumeration cats = LogManager.getCurrentLoggers();
92 while (cats.hasMoreElements()) {
93 printOptions(out, (Logger) cats.nextElement());
101 void printOptions(PrintWriter out, Category cat) {
102 Enumeration appenders = cat.getAllAppenders();
103 Level prio = cat.getLevel();
104 String appenderString = (prio == null ? "" : prio.toString());
106 while (appenders.hasMoreElements()) {
107 Appender app = (Appender) appenders.nextElement();
110 if ((name = (String) appenderNames.get(app)) == null) {
112 // first assign name to the appender
113 if ((name = app.getName()) == null || isGenAppName(name)) {
116 appenderNames.put(app, name);
118 printOptions(out, app, "log4j.appender."+name);
119 if (app.getLayout() != null) {
120 printOptions(out, app.getLayout(), "log4j.appender."+name+".layout");
123 appenderString += ", " + name;
125 String catKey = (cat == Logger.getRootLogger())
127 : "log4j.logger." + cat.getName();
128 if (appenderString != "") {
129 out.println(catKey + "=" + appenderString);
131 if (!cat.getAdditivity() && cat != Logger.getRootLogger()) {
132 out.println("log4j.additivity." + cat.getName() + "=false");
136 protected void printOptions(PrintWriter out, Logger cat) {
137 printOptions(out, (Category) cat);
141 void printOptions(PrintWriter out, Object obj, String fullname) {
142 out.println(fullname + "=" + obj.getClass().getName());
143 PropertyGetter.getProperties(obj, this, fullname + ".");
146 public void foundProperty(Object obj, String prefix, String name, Object value) {
147 // XXX: Properties encode value.toString()
148 if (obj instanceof Appender && "name".equals(name)) {
152 name = capitalize(name);
154 out.println(prefix + name + "=" + value.toString());
157 public static String capitalize(String name) {
158 if (Character.isLowerCase(name.charAt(0))) {
159 if (name.length() == 1 || Character.isLowerCase(name.charAt(1))) {
160 StringBuffer newname = new StringBuffer(name);
161 newname.setCharAt(0, Character.toUpperCase(name.charAt(0)));
162 return newname.toString();
169 public static void main(String[] args) {
170 new PropertyPrinter(new PrintWriter(System.out));