1 package org.apache.ojb.broker.util.logging; 2 3 17 18 import java.util.HashMap ; 19 import java.util.Map ; 20 21 import org.apache.ojb.broker.util.ClassHelper; 22 import org.apache.commons.lang.SystemUtils; 23 24 36 public class LoggerFactoryImpl 37 { 38 public static final String BOOT_LOG_LEVEL_STR = "OJB.bootLogLevel"; 39 protected static final String BOOT_STR = "BOOT"; 40 protected static final String DEFAULT_STR = "DEFAULT"; 41 protected static final LoggerFactoryImpl INSTANCE = new LoggerFactoryImpl(); 42 43 private Logger defaultLogger = null; 44 45 private Logger bootLogger = null; 46 47 private boolean bootLoggerIsReassigned = false; 48 49 50 private Map cache = new HashMap (); 51 52 private LoggingConfiguration conf; 53 54 private LoggerFactoryImpl() 56 { 57 } 58 59 public static LoggerFactoryImpl getInstance() 60 { 61 return INSTANCE; 62 } 63 64 private LoggingConfiguration getConfiguration() 65 { 66 if(conf == null) 67 { 68 conf = new LoggingConfiguration(); 70 } 71 return conf; 72 } 73 74 81 public Logger getBootLogger() 82 { 83 if(bootLogger == null) 84 { 85 bootLogger = createStringBufferLogger_Boot(); 87 } 88 return bootLogger; 89 } 90 91 92 99 public Logger getDefaultLogger() 100 { 101 if(defaultLogger == null) 102 { 103 defaultLogger = getLogger(DEFAULT_STR); 104 } 105 return defaultLogger; 106 } 107 108 109 116 public Logger getLogger(Class clazz) 117 { 118 return getLogger(clazz.getName()); 119 } 120 121 122 128 public Logger getLogger(String loggerName) 129 { 130 Logger logger; 131 logger = (Logger) cache.get(loggerName); 133 134 if(logger == null) 135 { 136 try 137 { 138 logger = createLoggerInstance(loggerName); 140 if(getBootLogger().isDebugEnabled()) 141 { 142 getBootLogger().debug("Using logger class '" 143 + (getConfiguration() != null ? getConfiguration().getLoggerClass() : null) 144 + "' for " + loggerName); 145 } 146 getBootLogger().debug("Initializing logger instance " + loggerName); 148 logger.configure(conf); 149 } 150 catch(Throwable t) 151 { 152 reassignBootLogger(true); 154 logger = getBootLogger(); 155 getBootLogger().error("[" + this.getClass().getName() 156 + "] Could not initialize logger " + (conf != null ? conf.getLoggerClass() : null), t); 157 } 158 cache.put(loggerName, logger); 160 reassignBootLogger(false); 162 } 163 return logger; 164 } 165 166 169 private Logger createLoggerInstance(String loggerName) throws Exception 170 { 171 Class loggerClass = getConfiguration().getLoggerClass(); 172 Logger log = (Logger) ClassHelper.newInstance(loggerClass, String .class, loggerName); 173 log.configure(getConfiguration()); 174 return log; 175 } 176 177 181 protected synchronized void reassignBootLogger(boolean forceError) 182 { 183 if(!bootLoggerIsReassigned) 185 { 186 Logger newBootLogger = null; 187 String name = getBootLogger().getName(); 188 try 189 { 190 newBootLogger = createLoggerInstance(name); 192 } 193 catch(Exception e) {} 194 if(newBootLogger == null) 195 { 196 newBootLogger = createPoorMansLogger_Boot(); 198 } 199 if(getBootLogger() instanceof StringBufferLoggerImpl) 200 { 201 205 StringBufferLoggerImpl strLogger = (StringBufferLoggerImpl) getBootLogger(); 206 String bootMessage = strLogger.flushLogBuffer(); 207 String eol = SystemUtils.LINE_SEPARATOR; 208 if(forceError || strLogger.isErrorLog()) 209 { 210 newBootLogger.error("-- boot log messages -->" + eol + bootMessage); 211 } 212 else 213 { 214 newBootLogger.info("-- boot log messages -->" + eol + bootMessage); 215 } 216 } 217 bootLogger = newBootLogger; 218 bootLoggerIsReassigned = true; 219 } 220 } 221 222 protected Logger createPoorMansLogger_Boot() 223 { 224 Logger bootLogger = new PoorMansLoggerImpl(BOOT_STR); 225 String level = System.getProperty(BOOT_LOG_LEVEL_STR, LoggingConfiguration.OJB_DEFAULT_BOOT_LOG_LEVEL); 227 ((PoorMansLoggerImpl) bootLogger).setLevel(level); 228 return bootLogger; 229 } 230 231 protected Logger createStringBufferLogger_Boot() 232 { 233 Logger bootLogger = new StringBufferLoggerImpl(BOOT_STR); 234 String level = System.getProperty(BOOT_LOG_LEVEL_STR, LoggingConfiguration.OJB_DEFAULT_BOOT_LOG_LEVEL); 236 ((PoorMansLoggerImpl) bootLogger).setLevel(level); 237 return bootLogger; 238 } 239 } 240 | Popular Tags |