1 18 19 package org.objectweb.util.monolog.wrapper.log4jMini; 20 21 import org.apache.log4j.Category; 22 import org.apache.log4j.PropertyConfigurator; 23 import org.objectweb.util.monolog.api.BasicLevel; 24 import org.objectweb.util.monolog.api.Handler; 25 import org.objectweb.util.monolog.api.HandlerFactory; 26 import org.objectweb.util.monolog.api.Level; 27 import org.objectweb.util.monolog.api.LevelFactory; 28 import org.objectweb.util.monolog.api.Logger; 29 import org.objectweb.util.monolog.api.LoggerFactory; 30 import org.objectweb.util.monolog.api.TopicalLogger; 31 import org.objectweb.util.monolog.api.MonologFactory; 32 import org.objectweb.util.monolog.wrapper.common.Configurable; 33 34 import java.util.Hashtable ; 35 import java.util.Properties ; 36 import java.util.Vector ; 37 import java.util.Enumeration ; 38 import java.io.FileInputStream ; 39 import java.io.IOException ; 40 import java.io.InputStream ; 41 import java.io.FileNotFoundException ; 42 43 public class MonologLoggerFactory implements MonologFactory { 44 45 static Hashtable monologCategories; 46 47 public static final String LOG4J_CF_PROP = "log4j.categoryFactory"; 48 public static final String LOG4J_CF_VALUE 49 = "org.objectweb.util.monolog.wrapper.log4j.MonologCategoryFactory"; 50 51 54 static { 55 BasicLevel.INHERIT = -1; 56 BasicLevel.DEBUG = org.apache.log4j.Priority.DEBUG_INT; 57 BasicLevel.INFO = org.apache.log4j.Priority.INFO_INT; 58 BasicLevel.WARN = org.apache.log4j.Priority.WARN_INT; 59 BasicLevel.ERROR = org.apache.log4j.Priority.ERROR_INT; 60 BasicLevel.FATAL = org.apache.log4j.Priority.FATAL_INT; 61 62 BasicLevel.LEVEL_INHERIT = new LevelImpl("INHERIT", BasicLevel.INHERIT); 63 BasicLevel.LEVEL_DEBUG = new LevelImpl("DEBUG", BasicLevel.DEBUG); 64 BasicLevel.LEVEL_INFO = new LevelImpl("INFO", BasicLevel.INFO); 65 BasicLevel.LEVEL_WARN = new LevelImpl("WARN", BasicLevel.WARN); 66 BasicLevel.LEVEL_ERROR = new LevelImpl("ERROR", BasicLevel.ERROR); 67 BasicLevel.LEVEL_FATAL = new LevelImpl("FATAL", BasicLevel.FATAL); 68 69 monologCategories = new Hashtable (); 70 Category root = Category.getRoot(); 71 } 72 73 78 protected Hashtable nameToLevel = null; 79 80 87 protected Hashtable intToNames = null; 88 89 94 protected Hashtable handlers = null; 95 96 99 protected Logger rootLogger = null; 100 101 public MonologLoggerFactory() { 102 intToNames = new Hashtable (); 103 nameToLevel = new Hashtable (); 104 defineDefaultLevels(); 105 handlers = new Hashtable (); 106 rootLogger = new MonologCategory(Category.getRoot()); 107 } 108 109 119 protected void defineDefaultLevels() { 120 defineLevel("INHERIT", -1); 121 defineLevel("DEBUG", 10000); 122 defineLevel("INFO", 20000); 123 defineLevel("WARN", 30000); 124 defineLevel("ERROR", 40000); 125 defineLevel("FATAL", 50000); 126 } 127 128 130 133 public void configure(Properties prop) throws Exception { 134 if (prop==null) { 135 return; 136 } 137 String conf = prop.getProperty(LOG_CONFIGURATION_TYPE, 138 prop.getProperty("log4jConfiguration", DEFAULT)); 139 if (PROPERTY.equals(conf)) { 140 141 String filename = prop.getProperty(LOG_CONFIGURATION_FILE, 143 prop.getProperty("log4jConfigurationFile", "")); 144 Properties log4jfileprop = null; 145 if (prop.getProperty(LOG_CONFIGURATION_FILE_USE_CLASSPATH, "false") 149 .equalsIgnoreCase("true") 150 || prop.getProperty("findFileInClassPath", "false") 151 .equalsIgnoreCase("true")) { 152 153 if (!"log4j.properties".equals(filename)) { 154 log4jfileprop = getProperties(filename); 156 log4jfileprop.put(LOG4J_CF_PROP, LOG4J_CF_VALUE); 157 PropertyConfigurator.configure(log4jfileprop); 158 } 159 } 161 else { 162 log4jfileprop = new Properties (); 164 log4jfileprop.load(new FileInputStream (filename)); 165 log4jfileprop.put(LOG4J_CF_PROP, LOG4J_CF_VALUE); 166 PropertyConfigurator.configure(log4jfileprop); 167 } 168 } 169 else if (XML.equals(conf)) { 170 throw new Exception ( 171 "xml log4jConfiguration is not suported in the log4jME wrapper"); 172 } 173 } 174 175 private Properties getProperties(String name) throws IOException { 177 InputStream is = ClassLoader.getSystemResourceAsStream(name); 178 if (is != null) { 179 Properties props = new Properties (); 180 props.load(is); 181 return props; 182 } 183 throw new FileNotFoundException ("Not found in classpath: " + name); 184 } 185 186 public Logger[] getLoggers() { 188 Logger[] logs = new TopicalLogger[monologCategories.size() + 1]; 189 logs[0] = rootLogger; 190 int i=1; 191 for(Enumeration en = monologCategories.elements(); en.hasMoreElements();) { 192 logs[i++] = (TopicalLogger) en.nextElement(); 193 } 194 return logs; 195 } 196 public String getTopicPrefix() { 197 return null; 198 } 199 200 public Logger getLogger(String key) { 201 if (key == null || key.length() == 0 || key.equalsIgnoreCase("root")) { 202 return rootLogger; 203 } 204 Logger log = (Logger) monologCategories.get(key); 205 if (log == null) { 206 log = new MonologCategory(Category.getInstance(key)); 207 monologCategories.put(key, log); 208 } 209 return log; 210 } 211 212 public synchronized Logger getLogger(String key, String rbn) { 213 return getLogger(key); 214 } 215 216 public String getResourceBundleName() { 217 return ""; 218 } 219 220 public void setResourceBundleName(String rbn) { 221 } 222 223 226 public Level defineLevel(String name, int value) { 227 return defineLevel(new LevelImpl(name, value)); 228 } 229 230 public Level defineLevel(String name, String value) { 231 return defineLevel(new LevelImpl(name, value, this)); 232 } 233 234 public Level getLevel(String name) { 235 return (Level) nameToLevel.get(name); 236 } 237 238 public Level getLevel(int value) { 239 Object temp = intToNames.get(new Integer (value)); 240 if (temp == null) { 241 return null; 242 } 243 else if (temp instanceof String ) { 244 return getLevel((String ) temp); 245 } 246 else if (temp instanceof Vector ) { 247 return getLevel((String ) ((Vector ) temp).elementAt(0)); 248 } 249 return null; 250 } 251 252 public Level[] getLevels() { 253 if (nameToLevel==null) { 254 return new Level[0]; 255 } 256 Level[] result = new Level[nameToLevel.size()]; 257 int i = 0; 258 for (Enumeration e= nameToLevel.elements(); e.hasMoreElements() ; i++) { 259 result[i] = (Level)(e.nextElement()); 260 } 261 return result; 262 } 263 264 public void removeLevel(String name) { 265 Level removed = (Level) nameToLevel.remove(name); 266 if (removed != null) { 267 Integer i = new Integer (removed.getIntValue()); 268 Object temp = intToNames.get(i); 269 if (temp instanceof String ) { 270 intToNames.remove(i); 271 } 272 else if (temp instanceof Vector ) { 273 ((Vector ) temp).removeElement(name); 274 } 275 } 276 } 277 280 290 private Level defineLevel(Level l) { 291 String name = l.getName(); 293 int value = l.getIntValue(); 294 Level res = (Level) nameToLevel.get(name); 295 if (res != null) { 296 return (res.getIntValue() == value ? res : null); 298 } 299 else { 300 res = l; 301 nameToLevel.put(name, res); 302 Integer i = new Integer (value); 303 Object temp = intToNames.get(i); 304 if (temp != null) { 305 if (temp instanceof String ) { 306 if (!((String ) temp).equalsIgnoreCase(name)) { 307 Vector al = new Vector (5); 310 al.addElement(temp); 311 al.addElement(name); 312 intToNames.put(i, al); 313 } 314 } 315 else if (temp instanceof Vector ) { 316 Vector al = (Vector ) temp; 318 if (!al.contains(name)) { 319 al.addElement(name); 321 } 322 } 323 } 324 else { 325 intToNames.put(i, name); 327 } 328 } 329 return res; 331 } 332 335 public Handler createHandler(String hn, String handlertype) { 336 Handler res = (Handler) handlers.get(hn); 337 if (res != null) { 338 return null; 339 } 340 res = new FileHandler(handlertype, hn); 341 handlers.put(hn, res); 342 return res; 343 } 344 345 public Handler[] getHandlers() { 346 if (handlers==null) { 347 return new Handler[0]; 348 } 349 Handler[] result = new Handler[handlers.size()]; 350 int i = 0; 351 for (Enumeration e= handlers.elements(); e.hasMoreElements() ; i++) { 352 result[i] = (Handler)(e.nextElement()); 353 } 354 return result; 355 } 356 357 public Handler getHandler(String hn) { 358 return (Handler) handlers.get(hn); 359 } 360 361 public Handler removeHandler(String hn) { 362 return (Handler) handlers.remove(hn); 363 } 364 } | Popular Tags |