1 22 39 package org.objectweb.petals.util.monolog.wrapper.javalog; 40 41 import java.util.ArrayList ; 42 import java.util.Enumeration ; 43 import java.util.List ; 44 import java.util.Properties ; 45 import java.util.logging.LogManager ; 46 47 import org.objectweb.util.monolog.Monolog; 48 import org.objectweb.util.monolog.api.BasicLevel; 49 import org.objectweb.util.monolog.wrapper.common.AbstractFactory; 50 import org.objectweb.util.monolog.wrapper.javaLog.LevelImpl; 51 52 57 public class LoggerFactory extends AbstractFactory { 58 59 protected String [][] getDefaultHandlerType2className() { 60 return new String [][] { 61 {handlerTypes[0], 62 "org.objectweb.petals.util.monolog.wrapper.javalog.GenericHandler"}, 63 {handlerTypes[1], 64 "org.objectweb.petals.util.monolog.wrapper.javalog.GenericHandler"}, 65 {handlerTypes[2], 66 "org.objectweb.petals.util.monolog.wrapper.javalog.GenericHandler"}, 67 {handlerTypes[3], 68 "org.objectweb.petals.util.monolog.wrapper.javalog.GenericHandler"}, 69 {handlerTypes[4], 70 "org.objectweb.util.monolog.wrapper.javaLog.JMXGenericHandler"}}; 71 } 72 73 76 protected static LogManager manager = null; 77 78 81 protected static Logger rootLogger = null; 82 83 88 private List <Logger> loggers = new ArrayList <Logger>(); 89 90 94 static { 95 debug("Initializing " + LoggerFactory.class.getName()); 96 debug("set default level values"); 97 BasicLevel.INHERIT = -1; 98 debug("\t-INHERIT= " + BasicLevel.INHERIT); 99 BasicLevel.DEBUG = java.util.logging.Level.FINEST.intValue(); 100 debug("\t-DEBUG= " + BasicLevel.DEBUG); 101 BasicLevel.INFO = java.util.logging.Level.INFO.intValue(); 102 debug("\t-INFO= " + BasicLevel.INFO); 103 BasicLevel.WARN = java.util.logging.Level.WARNING.intValue(); 104 debug("\t-WARN= " + BasicLevel.WARN); 105 BasicLevel.ERROR = java.util.logging.Level.SEVERE.intValue(); 106 debug("\t-ERROR= " + BasicLevel.ERROR); 107 BasicLevel.FATAL = java.util.logging.Level.SEVERE.intValue(); 108 debug("\t-FATAL= " + BasicLevel.FATAL); 109 110 BasicLevel.LEVEL_INHERIT = new LevelImpl("INHERIT", BasicLevel.INHERIT); 111 BasicLevel.LEVEL_DEBUG = new LevelImpl("DEBUG", BasicLevel.DEBUG); 112 BasicLevel.LEVEL_INFO = new LevelImpl("INFO", BasicLevel.INFO); 113 BasicLevel.LEVEL_WARN = new LevelImpl("WARN", BasicLevel.WARN); 114 BasicLevel.LEVEL_ERROR = new LevelImpl("ERROR", BasicLevel.ERROR); 115 BasicLevel.LEVEL_FATAL = new LevelImpl("FATAL", BasicLevel.FATAL); 116 117 manager = LogManager.getLogManager(); 118 if (classLoaderIsoltion) { 119 debug("class loader isolation activated"); 120 int i = 0; 121 while (rootLogger == null) { 122 rootLoggerName = "root" + i; 123 synchronized (manager) { 124 if (manager.getLogger(rootLoggerName) == null) { 127 rootLogger = new Logger(rootLoggerName, null); 128 manager.addLogger(rootLogger); 129 } else { 130 i++; 131 } 132 } 133 } 134 rootLogger.setUseParentHandlers(false); 135 Monolog.debug("Instanciate the root logger " + rootLoggerName); 136 rootLoggerPrefix = rootLoggerName + '.'; 137 } else { 138 rootLogger = new Logger(manager.getLogger("")); 139 } 140 } 141 142 145 public final static String JAVALOG_CONFIGURATION = "javalogConfiguration"; 146 147 151 public final static String DEFAULT = "default"; 152 153 157 public final static String PROPERTY = "property"; 158 159 163 public final static String CLASS = "class"; 164 165 169 public final static String JAVALOG_CONFIGURATION_FILE = "javalogConfigurationFile"; 170 171 175 public final static String JAVALOG_CONFIGURATION_CLASS = "javalogConfigurationClass"; 176 177 public LoggerFactory() { 178 super(); 179 } 180 181 public String getWrapperName() { 182 return "javalog"; 183 } 184 185 190 protected synchronized Logger getMonoLogger(String name, String resName) { 191 if (name == null) 192 throw new IllegalArgumentException ( 193 "The specified Logger name is null"); 194 if (name.equals("root") || name.length() == 0) { 195 return rootLogger; 196 } 197 name = monoLoggerName(name); 199 Object o = manager.getLogger(name); 201 if (o == null) { 202 Logger result = new Logger(name, resName); 204 manager.addLogger(result); 205 Monolog.debug("Instanciate the logger " + name); 206 207 loggers.add(result); 211 return result; 212 } else if (!o.getClass().equals(Logger.class)) { 213 throw new RuntimeException ("Bad logger class (logger name: '" 214 + name + "'), expected: " + Logger.class + ", found: " 215 + o.getClass()); 216 } else { 217 Monolog.debug("return existing logger " + name); 218 return (Logger) o; 219 } 220 } 221 222 225 236 public void configure(Properties prop) throws Exception { 237 if (prop == null) 238 return; 239 String confMode = prop.getProperty(JAVALOG_CONFIGURATION, null); 240 if (confMode == null) 241 return; 242 243 String param = null; 244 if (confMode.equals(PROPERTY)) { 245 param = prop.getProperty(JAVALOG_CONFIGURATION_FILE, null); 246 if (param != null) 247 System.setProperty("java.util.logging.config.file", param); 248 manager.readConfiguration(); 249 } else if (confMode.equals(CLASS)) { 250 param = prop.getProperty(JAVALOG_CONFIGURATION_CLASS, null); 251 if (param != null) 252 System.setProperty("java.util.logging.config.class", param); 253 manager.readConfiguration(); 254 } 255 } 256 257 260 public org.objectweb.util.monolog.api.Logger getLogger(String key) { 261 return getMonoLogger(key, resourceBundleName); 262 } 263 264 public org.objectweb.util.monolog.api.Logger getLogger(String key, 265 String rbn) { 266 return getMonoLogger(key, rbn); 267 } 268 269 272 public org.objectweb.util.monolog.api.Logger[] getLoggers() { 273 List <Logger> res = new ArrayList <Logger>(); 274 for (Enumeration e = manager.getLoggerNames(); e.hasMoreElements();) { 275 Object o = manager.getLogger((String ) e.nextElement()); 276 if (o instanceof Logger) { 277 res.add((Logger) o); 278 } 279 } 280 return (Logger[]) res.toArray(new Logger[0]); 281 } 282 283 } 284 | Popular Tags |