1 23 package org.objectweb.medor.lib; 24 25 import org.objectweb.util.monolog.api.LoggerFactory; 26 import org.objectweb.util.monolog.wrapper.printwriter.LoggerImpl; 27 import org.objectweb.util.monolog.api.HandlerFactory; 28 import org.objectweb.util.monolog.api.LevelFactory; 29 import org.objectweb.util.monolog.api.Logger; 30 import org.objectweb.util.monolog.file.monolog.PropertiesConfAccess; 31 import org.objectweb.util.monolog.Monolog; 32 33 import java.io.File ; 34 import java.io.FileInputStream ; 35 import java.io.InputStream ; 36 import java.io.FileNotFoundException ; 37 import java.util.Properties ; 38 39 46 public class Log 47 implements LoggerFactory { 48 49 public static final boolean DEBUG = false; 50 51 public static final String MEDOR_PREFIX = "org.objectweb.medor"; 52 53 public static LoggerFactory loggerFactory = new LoggerImpl(); 54 55 58 public final static String DEFAULT_LOGGER_FACTORY = 59 "org.objectweb.util.monolog.wrapper.printwriter.LoggerImpl"; 60 61 public static LoggerFactory getLoggerFactory(String lfClassName) 62 throws Exception { 63 if (lfClassName == null) 64 return (LoggerFactory) 65 Class.forName(DEFAULT_LOGGER_FACTORY).newInstance(); 66 else 67 return (LoggerFactory) Class.forName(lfClassName).newInstance(); 68 } 69 70 77 public static LoggerFactory loadLoggerFactory(String propFileName) 78 throws Exception { 79 if (propFileName == null) 80 return (LoggerFactory) 81 Class.forName(DEFAULT_LOGGER_FACTORY).newInstance(); 82 File f = new File (propFileName); 84 if (f.exists()) { 85 Properties p = new Properties (); 86 p.load(new FileInputStream (propFileName)); 87 return loadLoggerFactory(p); 88 } 89 InputStream is = ClassLoader.getSystemResourceAsStream(propFileName); 91 if (is != null) { 92 Properties p = new Properties (); 93 p.load(is); 94 return loadLoggerFactory(p); 95 } 96 throw new FileNotFoundException (propFileName + " was not found"); 97 } 98 99 105 public static LoggerFactory loadLoggerFactory(Properties prop) throws Exception { 106 if (prop == null) 107 return (LoggerFactory) 108 Class.forName(DEFAULT_LOGGER_FACTORY).newInstance(); 109 110 String lfn = prop.getProperty("log.config.classname", DEFAULT_LOGGER_FACTORY); 112 LoggerFactory factory = (LoggerFactory) Class.forName(lfn).newInstance(); 113 114 if (factory instanceof HandlerFactory && factory instanceof LevelFactory) { 115 PropertiesConfAccess.load(prop, 117 factory, 118 (HandlerFactory) factory, 119 (LevelFactory) factory); 120 } 121 return factory; 122 } 123 124 public static LoggerFactory getLoggerFactory() { 125 if (lastLF == null || lastLF == Monolog.getDefaultMonologFactory()) { 126 lastLF = Monolog.initialize(); 127 } 128 return lastLF; 129 } 130 131 private static LoggerFactory lastLF = null; 132 133 private LoggerFactory delegate = null; 134 private String pfn = null; 135 136 139 public String getPropertiesFileName() { 140 return pfn; 141 } 142 143 public void setPropertiesFileName(String propfn) { 144 if (pfn != null && pfn.equals(propfn)) 145 return; 146 pfn = propfn; 147 try { 148 delegate = Log.loadLoggerFactory(pfn); 149 } catch (Exception e) { 150 e.printStackTrace(); 151 try { 152 delegate = Log.loadLoggerFactory((String ) null); 153 } catch (Exception e1) { 154 e1.printStackTrace(); 155 } 156 } 157 lastLF = this; 158 } 159 160 163 public Logger getLogger(String s) { 164 return delegate.getLogger(s); 165 } 166 167 public Logger getLogger(String s, String s1) { 168 return delegate.getLogger(s1); 169 } 170 171 public String getResourceBundleName() { 172 return delegate.getResourceBundleName(); 173 } 174 175 public void setResourceBundleName(String s) { 176 delegate.setResourceBundleName(s); 177 } 178 179 public Logger[] getLoggers() { 180 return delegate.getLoggers(); 181 } 182 } 183 | Popular Tags |