1 16 17 package org.apache.log4j.config; 18 19 import java.io.*; 20 import java.util.*; 21 import org.apache.log4j.*; 22 23 30 public class PropertyPrinter implements PropertyGetter.PropertyCallback { 31 protected int numAppenders = 0; 32 protected Hashtable appenderNames = new Hashtable(); 33 protected Hashtable layoutNames = new Hashtable(); 34 protected PrintWriter out; 35 protected boolean doCapitalize; 36 37 public 38 PropertyPrinter(PrintWriter out) { 39 this(out, false); 40 } 41 42 public 43 PropertyPrinter(PrintWriter out, boolean doCapitalize) { 44 this.out = out; 45 this.doCapitalize = doCapitalize; 46 47 print(out); 48 out.flush(); 49 } 50 51 protected 52 String genAppName() { 53 return "A" + numAppenders++; 54 } 55 56 60 protected 61 boolean isGenAppName(String name) { 62 if (name.length() < 2 || name.charAt(0) != 'A') return false; 63 64 for (int i = 0; i < name.length(); i++) { 65 if (name.charAt(i) < '0' || name.charAt(i) > '9') return false; 66 } 67 return true; 68 } 69 70 76 public 77 void print(PrintWriter out) { 78 printOptions(out, Logger.getRootLogger()); 79 80 Enumeration cats = LogManager.getCurrentLoggers(); 81 while (cats.hasMoreElements()) { 82 printOptions(out, (Logger) cats.nextElement()); 83 } 84 } 85 86 protected 87 void printOptions(PrintWriter out, Logger cat) { 88 Enumeration appenders = cat.getAllAppenders(); 89 Level prio = cat.getLevel(); 90 String appenderString = (prio == null ? "" : prio.toString()); 91 92 while (appenders.hasMoreElements()) { 93 Appender app = (Appender) appenders.nextElement(); 94 String name; 95 96 if ((name = (String ) appenderNames.get(app)) == null) { 97 98 if ((name = app.getName()) == null || isGenAppName(name)) { 100 name = genAppName(); 101 } 102 appenderNames.put(app, name); 103 104 printOptions(out, app, "log4j.appender."+name); 105 if (app.getLayout() != null) { 106 printOptions(out, app.getLayout(), "log4j.appender."+name+".layout"); 107 } 108 } 109 appenderString += ", " + name; 110 } 111 String catKey = (cat == Logger.getRootLogger()) 112 ? "log4j.rootLogger" 113 : "log4j.logger." + cat.getName(); 114 if (appenderString != "") { 115 out.println(catKey + "=" + appenderString); 116 } 117 } 118 119 protected 120 void printOptions(PrintWriter out, Object obj, String fullname) { 121 out.println(fullname + "=" + obj.getClass().getName()); 122 PropertyGetter.getProperties(obj, this, fullname + "."); 123 } 124 125 public void foundProperty(Object obj, String prefix, String name, Object value) { 126 if (obj instanceof Appender && "name".equals(name)) { 128 return; 129 } 130 if (doCapitalize) { 131 name = capitalize(name); 132 } 133 out.println(prefix + name + "=" + value.toString()); 134 } 135 136 public static String capitalize(String name) { 137 if (Character.isLowerCase(name.charAt(0))) { 138 if (name.length() == 1 || Character.isLowerCase(name.charAt(1))) { 139 StringBuffer newname = new StringBuffer (name); 140 newname.setCharAt(0, Character.toUpperCase(name.charAt(0))); 141 return newname.toString(); 142 } 143 } 144 return name; 145 } 146 147 public static void main(String [] args) { 149 new PropertyPrinter(new PrintWriter(System.out)); 150 } 151 } 152 | Popular Tags |