1 18 package org.objectweb.util.monolog.wrapper.log4j; 19 20 import org.apache.log4j.PropertyConfigurator; 21 import org.apache.log4j.BasicConfigurator; 22 import org.apache.log4j.LogManager; 23 import org.apache.log4j.xml.DOMConfigurator; 24 import org.objectweb.util.monolog.api.BasicLevel; 25 import org.objectweb.util.monolog.api.Logger; 26 import org.objectweb.util.monolog.api.TopicalLogger; 27 import org.objectweb.util.monolog.wrapper.common.AbstractFactory; 28 import org.objectweb.util.monolog.Monolog; 29 30 import java.io.FileInputStream ; 31 import java.io.FileNotFoundException ; 32 import java.io.IOException ; 33 import java.io.InputStream ; 34 import java.util.Enumeration ; 35 import java.util.Properties ; 36 import java.util.ResourceBundle ; 37 import java.util.HashSet ; 38 39 46 public class MonologLoggerFactory 47 extends AbstractFactory { 48 49 public static final String LOG4J_CF_PROP = "log4j.categoryFactory"; 50 public static final String LOG4J_CF_VALUE 51 = "org.objectweb.util.monolog.wrapper.log4j.MonologCategoryFactory"; 52 53 56 protected static Logger rootLogger = null; 57 58 61 private static org.apache.log4j.spi.LoggerFactory factory 62 = new MonologCategoryFactory(); 63 64 67 static { 68 BasicLevel.INHERIT = -1; 69 debug("INHERIT= " + BasicLevel.INHERIT); 70 BasicLevel.DEBUG = org.apache.log4j.Level.DEBUG.toInt(); 71 debug("DEBUG= " + BasicLevel.DEBUG); 72 BasicLevel.INFO = org.apache.log4j.Level.INFO.toInt(); 73 debug("INFO= " + BasicLevel.INFO); 74 BasicLevel.WARN = org.apache.log4j.Level.WARN.toInt(); 75 debug("WARN= " + BasicLevel.WARN); 76 BasicLevel.ERROR = org.apache.log4j.Level.ERROR.toInt(); 77 debug("ERROR= " + BasicLevel.ERROR); 78 BasicLevel.FATAL = org.apache.log4j.Level.FATAL.toInt(); 79 debug("FATAL= " + BasicLevel.FATAL); 80 81 BasicLevel.LEVEL_INHERIT = new LevelImpl("INHERIT", BasicLevel.INHERIT); 82 BasicLevel.LEVEL_DEBUG = new LevelImpl("DEBUG", BasicLevel.DEBUG); 83 BasicLevel.LEVEL_INFO = new LevelImpl("INFO", BasicLevel.INFO); 84 BasicLevel.LEVEL_WARN = new LevelImpl("WARN", BasicLevel.WARN); 85 BasicLevel.LEVEL_ERROR = new LevelImpl("ERROR", BasicLevel.ERROR); 86 BasicLevel.LEVEL_FATAL = new LevelImpl("FATAL", BasicLevel.FATAL); 87 88 if (!org.apache.log4j.Logger.getRootLogger().getAllAppenders().hasMoreElements()) { 89 BasicConfigurator.configure(); 90 } 91 if (classLoaderIsoltion) { 92 String rootName = null; 93 int i = 0; 94 while (rootLogger == null) { 95 rootName = "root" + i; 96 try { 97 org.apache.log4j.Logger rlog = 98 org.apache.log4j.Logger.getLogger(rootName, factory); 99 rlog.setAdditivity(false); 100 rootLogger = (Logger) rlog; 101 Monolog.debug("Instanciate the root logger " + rootName); 102 } catch (ClassCastException exc) { 103 i ++; 106 } 107 } 108 rootLoggerName = rootName; 109 rootLoggerPrefix = rootLoggerName + '.'; 110 } 111 } 112 113 117 public MonologLoggerFactory() { 118 super(); 119 if (!classLoaderIsoltion) { 120 rootLogger = new MonologCategory( 121 org.apache.log4j.Logger.getRootLogger()); 122 } 123 } 124 125 public String getWrapperName() { 126 return "log4j"; 127 } 128 129 protected String [][] getDefaultHandlerType2className() { 130 return new String [][] { 131 {handlerTypes[0], "org.objectweb.util.monolog.wrapper.log4j.ConsoleHandler"}, 132 {handlerTypes[1], "org.objectweb.util.monolog.wrapper.log4j.FileHandler"}, 133 {handlerTypes[2], "org.objectweb.util.monolog.wrapper.log4j.RollingFileHandler"}, 134 {handlerTypes[3], "org.objectweb.util.monolog.wrapper.log4j.NTEventLogHandler"}, 135 {handlerTypes[4], "org.objectweb.util.monolog.wrapper.log4j.JMXHandler"} 136 }; 137 } 138 139 141 145 public void configure(Properties prop) throws Exception { 146 debug("MonologLoggerFactory.configure(prop=" + prop + ")"); 147 if (prop == null) { 148 if (!org.apache.log4j.Logger.getRootLogger().getAllAppenders().hasMoreElements()) { 149 BasicConfigurator.configure(); 150 } 151 return; 152 } 153 154 String conf = prop.getProperty(LOG_CONFIGURATION_TYPE, 155 prop.getProperty("log4jConfiguration", DEFAULT)); 156 debug("MonologLoggerFactory.configure(): conf=" + conf); 157 158 if (DEFAULT.equals(conf)) { 159 if (!org.apache.log4j.Logger.getRootLogger().getAllAppenders().hasMoreElements()) { 160 BasicConfigurator.configure(); 161 } 162 return; 163 } 164 165 String filename = prop.getProperty(LOG_CONFIGURATION_FILE, 167 prop.getProperty("log4jConfigurationFile", "")); 168 debug("MonologLoggerFactory.configure(): filename=" + filename); 169 170 if (XML.equals(conf)) { 171 DOMConfigurator.configure(filename); 172 } 173 else if (PROPERTY.equals(conf)) { 174 Properties log4jfileprop = null; 175 if (prop.getProperty(LOG_CONFIGURATION_FILE_USE_CLASSPATH, "false") 179 .equalsIgnoreCase("true") 180 || prop.getProperty("findFileInClassPath", "false") 181 .equalsIgnoreCase("true")) { 182 183 debug("MonologLoggerFactory.configure(): load from classpath"); 184 185 if (!LogManager.DEFAULT_CONFIGURATION_FILE.equals(filename)) { 186 debug("MonologLoggerFactory.configure(): not default config file"); 187 log4jfileprop = getProperties(filename); 189 log4jfileprop.setProperty(LOG4J_CF_PROP, LOG4J_CF_VALUE); 190 PropertyConfigurator.configure(log4jfileprop); 191 } 192 } 194 else { 195 debug("MonologLoggerFactory.configure(): load from file system"); 196 log4jfileprop = new Properties (); 198 log4jfileprop.load(new FileInputStream (filename)); 199 log4jfileprop.setProperty(LOG4J_CF_PROP, LOG4J_CF_VALUE); 200 PropertyConfigurator.configure(log4jfileprop); 201 } 202 } 203 else { 204 throw new Exception ("Unsupported configuration type: " + conf); 205 } 206 debug("MonologLoggerFactory.configure(): End"); 207 } 208 209 private Properties getProperties(String name) throws IOException { 211 InputStream is = getClass().getClassLoader().getResourceAsStream(name); 212 if (is != null) { 213 Properties props = new Properties (); 214 props.load(is); 215 return props; 216 } 217 throw new FileNotFoundException ("Not found in classpath: " + name); 218 } 219 220 221 224 public Logger getLogger(String key) { 225 if (key == null || key.length() == 0 || key.equalsIgnoreCase("root")) { 226 return rootLogger; 227 } 228 key = monoLoggerName(key); 230 if (resourceBundleName == null) 231 return (Logger) org.apache.log4j.Logger.getLogger(key, factory); 232 else 233 return getLogger(key, resourceBundleName); 234 } 235 236 public synchronized Logger getLogger(String key, String rbn) { 237 if (key == null || key.length() == 0 || key.equalsIgnoreCase("root")) { 238 return rootLogger; 239 } 240 key = monoLoggerName(key); 242 org.apache.log4j.Logger res = 243 org.apache.log4j.Logger.getLogger(key, factory); 244 res.setResourceBundle(ResourceBundle.getBundle(rbn)); 245 return (Logger) res; 246 } 247 248 public Logger[] getLoggers() { 249 HashSet al = new HashSet (); 250 for (Enumeration e = org.apache.log4j.LogManager.getCurrentLoggers(); 251 e.hasMoreElements();) { 252 Object o = e.nextElement(); 253 if (o instanceof Logger) 254 al.add(o); 255 } 256 al.add(rootLogger); 257 return (Logger[]) al.toArray(new TopicalLogger[0]); 258 } 259 } 260 | Popular Tags |