| 1 | |
package org.apache.ojb.broker.util.logging; |
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 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 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 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 | |
|
| 55 | |
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 | |
|
| 69 | |
conf = new LoggingConfiguration(); |
| 70 | |
} |
| 71 | |
return conf; |
| 72 | |
} |
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
public Logger getBootLogger() |
| 82 | |
{ |
| 83 | |
if(bootLogger == null) |
| 84 | |
{ |
| 85 | |
|
| 86 | |
bootLogger = createStringBufferLogger_Boot(); |
| 87 | |
} |
| 88 | |
return bootLogger; |
| 89 | |
} |
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
public Logger getDefaultLogger() |
| 100 | |
{ |
| 101 | |
if(defaultLogger == null) |
| 102 | |
{ |
| 103 | |
defaultLogger = getLogger(DEFAULT_STR); |
| 104 | |
} |
| 105 | |
return defaultLogger; |
| 106 | |
} |
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
public Logger getLogger(Class clazz) |
| 117 | |
{ |
| 118 | |
return getLogger(clazz.getName()); |
| 119 | |
} |
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
public Logger getLogger(String loggerName) |
| 129 | |
{ |
| 130 | |
Logger logger; |
| 131 | |
|
| 132 | |
logger = (Logger) cache.get(loggerName); |
| 133 | |
|
| 134 | |
if(logger == null) |
| 135 | |
{ |
| 136 | |
try |
| 137 | |
{ |
| 138 | |
|
| 139 | |
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 | |
|
| 147 | |
getBootLogger().debug("Initializing logger instance " + loggerName); |
| 148 | |
logger.configure(conf); |
| 149 | |
} |
| 150 | |
catch(Throwable t) |
| 151 | |
{ |
| 152 | |
|
| 153 | |
reassignBootLogger(true); |
| 154 | |
logger = getBootLogger(); |
| 155 | |
getBootLogger().error("[" + this.getClass().getName() |
| 156 | |
+ "] Could not initialize logger " + (conf != null ? conf.getLoggerClass() : null), t); |
| 157 | |
} |
| 158 | |
|
| 159 | |
cache.put(loggerName, logger); |
| 160 | |
|
| 161 | |
reassignBootLogger(false); |
| 162 | |
} |
| 163 | |
return logger; |
| 164 | |
} |
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 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 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
protected synchronized void reassignBootLogger(boolean forceError) |
| 182 | |
{ |
| 183 | |
|
| 184 | |
if(!bootLoggerIsReassigned) |
| 185 | |
{ |
| 186 | |
Logger newBootLogger = null; |
| 187 | |
String name = getBootLogger().getName(); |
| 188 | |
try |
| 189 | |
{ |
| 190 | |
|
| 191 | |
newBootLogger = createLoggerInstance(name); |
| 192 | |
} |
| 193 | |
catch(Exception e) {} |
| 194 | |
if(newBootLogger == null) |
| 195 | |
{ |
| 196 | |
|
| 197 | |
newBootLogger = createPoorMansLogger_Boot(); |
| 198 | |
} |
| 199 | |
if(getBootLogger() instanceof StringBufferLoggerImpl) |
| 200 | |
{ |
| 201 | |
|
| 202 | |
|
| 203 | |
|
| 204 | |
|
| 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 | |
|
| 226 | |
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 | |
|
| 235 | |
String level = System.getProperty(BOOT_LOG_LEVEL_STR, LoggingConfiguration.OJB_DEFAULT_BOOT_LOG_LEVEL); |
| 236 | |
((PoorMansLoggerImpl) bootLogger).setLevel(level); |
| 237 | |
return bootLogger; |
| 238 | |
} |
| 239 | |
} |