1 18 19 package org.objectweb.util.monolog.wrapper.javaLog; 20 21 import org.objectweb.util.monolog.api.BasicLevel; 22 import org.objectweb.util.monolog.wrapper.common.AbstractFactory; 23 import org.objectweb.util.monolog.wrapper.javaLog.Logger; 24 import org.objectweb.util.monolog.Monolog; 25 26 import java.util.ArrayList ; 27 import java.util.Enumeration ; 28 import java.util.Properties ; 29 import java.util.logging.LogManager ; 30 31 36 public class LoggerFactory extends AbstractFactory { 37 38 41 protected static LogManager manager = null; 42 43 46 protected static Logger rootLogger = null; 47 48 49 53 private ArrayList loggers = new ArrayList (); 54 55 59 static { 60 BasicLevel.INHERIT = -1; 61 debug("INHERIT= " + BasicLevel.INHERIT); 62 BasicLevel.DEBUG = java.util.logging.Level.FINEST.intValue(); 63 debug("DEBUG= " + BasicLevel.DEBUG); 64 BasicLevel.INFO = java.util.logging.Level.INFO.intValue(); 65 debug("INFO= " + BasicLevel.INFO); 66 BasicLevel.WARN = java.util.logging.Level.WARNING.intValue(); 67 debug("WARN= " + BasicLevel.WARN); 68 BasicLevel.ERROR = java.util.logging.Level.SEVERE.intValue(); 69 debug("ERROR= " + BasicLevel.ERROR); 70 BasicLevel.FATAL = java.util.logging.Level.SEVERE.intValue(); 71 debug("FATAL= " + BasicLevel.FATAL); 72 73 BasicLevel.LEVEL_INHERIT = new LevelImpl("INHERIT", BasicLevel.INHERIT); 74 BasicLevel.LEVEL_DEBUG = new LevelImpl("DEBUG", BasicLevel.DEBUG); 75 BasicLevel.LEVEL_INFO = new LevelImpl("INFO", BasicLevel.INFO); 76 BasicLevel.LEVEL_WARN = new LevelImpl("WARN", BasicLevel.WARN); 77 BasicLevel.LEVEL_ERROR = new LevelImpl("ERROR", BasicLevel.ERROR); 78 BasicLevel.LEVEL_FATAL = new LevelImpl("FATAL", BasicLevel.FATAL); 79 80 manager = LogManager.getLogManager(); 81 if (classLoaderIsoltion) { 82 int i = 0; 83 while (rootLogger == null) { 84 rootLoggerName = "root" + i; 85 synchronized (manager) { 86 if (manager.getLogger(rootLoggerName) == null) { 88 rootLogger = new Logger(rootLoggerName, null); 89 manager.addLogger(rootLogger); 90 } else { 91 i ++; 92 } 93 } 94 } 95 rootLogger.setUseParentHandlers(false); 96 Monolog.debug("Instanciate the root logger " + rootLoggerName); 97 rootLoggerPrefix = rootLoggerName + '.'; 98 } else { 99 rootLogger = new Logger(manager.getLogger("")); 100 } 101 } 102 103 106 public final static String JAVALOG_CONFIGURATION = "javaLogConfiguration"; 107 108 112 public final static String DEFAULT = "default"; 113 114 118 public final static String PROPERTY = "property"; 119 120 124 public final static String CLASS = "class"; 125 126 130 public final static String JAVALOG_CONFIGURATION_FILE 131 = "javaLogConfigurationFile"; 132 133 137 public final static String JAVALOG_CONFIGURATION_CLASS 138 = "javaLogConfigurationClass"; 139 140 public LoggerFactory() { 141 super(); 142 } 143 144 public String getWrapperName() { 145 return "javaLog"; 146 } 147 148 protected String [][] getDefaultHandlerType2className() { 149 return new String [][] { 150 {handlerTypes[0], "org.objectweb.util.monolog.wrapper.javaLog.GenericHandler"}, 151 {handlerTypes[1], "org.objectweb.util.monolog.wrapper.javaLog.GenericHandler"}, 152 {handlerTypes[2], "org.objectweb.util.monolog.wrapper.javaLog.GenericHandler"}, 153 {handlerTypes[3], "org.objectweb.util.monolog.wrapper.javaLog.GenericHandler"}, 154 {handlerTypes[4], "org.objectweb.util.monolog.wrapper.javaLog.JMXGenericHandler"} 155 }; 156 } 157 158 163 protected synchronized Logger getMonoLogger(String name, String resName) { 164 if (name == null) 165 throw new IllegalArgumentException ( 166 "The specified Logger name is null"); 167 if (name.equals("root") || name.length()==0) { 168 return rootLogger; 169 } 170 name = monoLoggerName(name); 172 Object o = manager.getLogger(name); 174 if (o == null) { 175 Logger result = new Logger(name, resName); 177 manager.addLogger(result); 178 Monolog.debug("Instanciate the logger " + name); 179 180 loggers.add(result); 184 return result; 185 } else if (!o.getClass().equals(Logger.class)) { 186 throw new RuntimeException ("Bad logger class (logger name: '" 187 + name +"'), expected: " + Logger.class 188 + ", found: " + o.getClass()); 189 } else { 190 return (Logger) o; 191 } 192 } 193 194 195 198 210 public void configure(Properties prop) throws Exception { 211 if (prop == null) 212 return; 213 String confMode = prop.getProperty(JAVALOG_CONFIGURATION, null); 214 if (confMode == null) 215 return; 216 217 String param = null; 218 if (confMode.equals(PROPERTY)) { 219 param = prop.getProperty(JAVALOG_CONFIGURATION_FILE, null); 220 if (param != null) 221 System.setProperty("java.util.logging.config.file", param); 222 manager.readConfiguration(); 223 } else if (confMode.equals(CLASS)) { 224 param = prop.getProperty(JAVALOG_CONFIGURATION_CLASS, null); 225 if (param != null) 226 System.setProperty("java.util.logging.config.class", param); 227 manager.readConfiguration(); 228 } 229 } 230 231 232 235 public org.objectweb.util.monolog.api.Logger getLogger(String key) { 236 return getMonoLogger(key, resourceBundleName); 237 } 238 239 public org.objectweb.util.monolog.api.Logger getLogger(String key, String rbn) { 240 return getMonoLogger(key, rbn); 241 } 242 243 246 public org.objectweb.util.monolog.api.Logger[] getLoggers() { 247 ArrayList res = new ArrayList (); 248 for (Enumeration e = manager.getLoggerNames(); e.hasMoreElements();) { 249 Object o = manager.getLogger((String ) e.nextElement()); 250 if (o instanceof Logger) { 251 res.add(o); 252 } 253 } 254 return (Logger[]) res.toArray(new Logger[0]); 255 } 256 } 257 | Popular Tags |