1 18 package org.objectweb.util.monolog; 19 20 import org.objectweb.util.monolog.api.LoggerFactory; 21 import org.objectweb.util.monolog.api.HandlerFactory; 22 import org.objectweb.util.monolog.api.LevelFactory; 23 import org.objectweb.util.monolog.api.MonologFactory; 24 import org.objectweb.util.monolog.wrapper.printwriter.LoggerImpl; 25 import org.objectweb.util.monolog.file.monolog.PropertiesConfAccess; 26 27 import java.util.Properties ; 28 import java.io.File ; 29 import java.io.FileInputStream ; 30 import java.io.IOException ; 31 import java.io.InputStream ; 32 import java.io.FileNotFoundException ; 33 34 45 public class Monolog { 46 49 public static boolean debug = new Boolean ( 50 System.getProperty("monolog.debug")).booleanValue(); 51 52 55 private static MonologFactory basicMonologFactory = new LoggerImpl(); 56 57 61 public static MonologFactory monologFactory = basicMonologFactory; 62 63 66 public static LoggerFactory loggerFactory = monologFactory; 67 68 72 public static final String DEFAULT_MONOLOG_FILE = "monolog.properties"; 73 74 78 public static final String MONOLOG_FILE_NAME = "monolog.filename"; 79 80 84 public static final String MONOLOG_CLASS_NAME = "monolog.classname"; 85 86 89 public static final String LOG4J_WRAPPER_CLASS_NAME 90 = "org.objectweb.util.monolog.wrapper.log4j.MonologLoggerFactory"; 91 92 95 public static final String LOG4JMini_WRAPPER_CLASS_NAME 96 = "org.objectweb.util.monolog.wrapper.log4jMini.MonologLoggerFactory"; 97 98 102 public static final String JDK_WRAPPER_CLASS_NAME 103 = "org.objectweb.util.monolog.wrapper.javaLog.LoggerFactory"; 104 107 private static final String LOG4J_CLASS_NAME = "org.apache.log4j.Logger"; 108 109 112 private static final String LOG4JMini_CLASS_NAME = "org.apache.log4j.Category"; 113 114 118 private static final String JDK_CLASS_NAME = "java.util.logging.Logger"; 119 120 121 public static MonologFactory getMonologFactory() { 122 return monologFactory; 123 } 124 125 public static MonologFactory getDefaultMonologFactory() { 126 return basicMonologFactory; 127 } 128 129 137 public static MonologFactory initialize() { 138 if (monologFactory == basicMonologFactory) { 139 getMonologFactory(System.getProperty(MONOLOG_FILE_NAME, DEFAULT_MONOLOG_FILE)); 140 } 141 return monologFactory; 142 } 143 144 147 public static LoggerFactory init() { 148 return initialize(); 149 } 150 151 154 public static LoggerFactory init(String fileName) { 155 return getMonologFactory(fileName); 156 } 157 158 168 public static MonologFactory getMonologFactory(String fileName) { 169 Monolog.debug("DEBUG: debug is activated!"); 170 File f = new File (fileName); 171 InputStream is = null; 172 if (f.exists()) { 173 Monolog.debug("DEBUG: The file " + fileName 174 + " was found from the file system."); 175 try { 176 is = new FileInputStream (f); 177 } catch (FileNotFoundException e) { 178 Monolog.debug("ERROR: " + e.getMessage()); 179 is = null; 180 } 181 } else { 182 is = Monolog.class.getClassLoader().getResourceAsStream(fileName); 183 if (is != null) 184 Monolog.debug("DEBUG: The file " + fileName 185 + " was found from the classpath."); 186 } 187 if (is != null) { 188 Properties p = new Properties (); 189 try { 190 p.load(is); 191 } catch (IOException e) { 192 Monolog.debug("ERROR: problem to load properties from " + 193 "the input stream " + is); 194 if (debug) { 195 e.printStackTrace(System.out); 196 } 197 } 198 getMonologFactory(p); 199 } else { 200 Monolog.debug("DEBUG: The file " + fileName 201 + " was not found."); 202 monologFactory = instanciateMonologFactory(null); 203 } 204 return monologFactory; 205 } 206 207 217 public static MonologFactory getMonologFactory(Properties properties) { 218 monologFactory = instanciateMonologFactory(properties.getProperty( 219 MONOLOG_CLASS_NAME, System.getProperty(MONOLOG_CLASS_NAME))); 220 if (monologFactory != basicMonologFactory) { 221 loadMonologConfiguration(properties, monologFactory); 222 } 223 return monologFactory; 224 } 225 226 229 public static LoggerFactory init(Properties properties) { 230 return getMonologFactory(properties); 231 } 232 233 238 public static void loadMonologConfiguration(Properties properties) { 239 loadMonologConfiguration(properties, monologFactory); 240 } 241 242 248 public static void loadMonologConfiguration(Properties properties, MonologFactory mf) { 249 try { 250 PropertiesConfAccess.load(properties, mf, mf, mf); 251 } catch (Exception e) { 252 Monolog.error("WARN: problem to configure a loggerFactory:", e); 253 } 254 } 255 258 public static void loadMonologConfiguration(Properties properties, LoggerFactory lf) { 259 loadMonologConfiguration(properties, (MonologFactory) lf); 260 } 261 262 public static LoggerFactory getLoggerFactory(String className) { 263 return getMonologFactory(className); 264 } 265 266 public static LevelFactory getLevelFactory(String className) { 267 return getMonologFactory(className); 268 } 269 270 public static HandlerFactory getHandlerFactory(String className) { 271 return getMonologFactory(className); 272 } 273 274 290 public static MonologFactory instanciateMonologFactory(String className) { 291 if (className != null) { 293 try { 294 Monolog.debug("DEBUG: try to use the logger factory: " + className); 295 return (MonologFactory) Class.forName(className).newInstance(); 296 } catch (ClassNotFoundException e) { 297 Monolog.debug("WARN: The class " + className 298 + " was not found."); 299 } catch (Exception e) { 300 Monolog.debug("WARN: The class " + className 301 + " has no public empty constructor."); 302 } 303 } 304 try { 306 Monolog.debug("DEBUG: try to use log4j"); 307 Class.forName(LOG4J_CLASS_NAME); 308 return (MonologFactory) 309 Class.forName(LOG4J_WRAPPER_CLASS_NAME).newInstance(); 310 } catch (Exception e) { 311 Monolog.debug("DEBUG: log4j and its wrapper are not " + 312 "availlable:" + e.getMessage()); 313 if (debug) { 314 e.printStackTrace(System.out); 315 } 316 } 317 try { 319 Monolog.debug("DEBUG: try to use the jdk"); 320 Class.forName(JDK_CLASS_NAME); 321 return (MonologFactory) 322 Class.forName(JDK_WRAPPER_CLASS_NAME).newInstance(); 323 } catch (Exception e) { 324 Monolog.debug("DEBUG: java.util.logging and its wrapper " + 325 "are not availlable:" + e.getMessage()); 326 if (debug) { 327 e.printStackTrace(System.out); 328 } 329 } 330 331 try { 333 Monolog.debug("DEBUG: try to use the log4j mini"); 334 Class.forName(LOG4JMini_CLASS_NAME); 335 return (MonologFactory) 336 Class.forName(LOG4JMini_WRAPPER_CLASS_NAME).newInstance(); 337 } catch (Exception e) { 338 Monolog.debug("DEBUG: log4jMini and its wrapper are not " + 339 "availlable:" + e.getMessage()); 340 if (debug) { 341 e.printStackTrace(System.out); 342 } 343 } 344 Monolog.debug("DEBUG: return the basic logger factory"); 345 return basicMonologFactory; 347 } 348 355 static public void debug(String m) { 356 if (debug) { 357 System.out.println(m); 358 } 359 } 360 static public void error(String m, Exception e) { 361 System.err.println(m); 362 if (debug) { 363 e.printStackTrace(System.err); 364 } 365 } 366 } 367 | Popular Tags |