| 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.io.InputStream; |
| 19 | |
import java.io.File; |
| 20 | |
import java.util.Properties; |
| 21 | |
import java.net.URL; |
| 22 | |
|
| 23 | |
import org.apache.ojb.broker.util.ClassHelper; |
| 24 | |
import org.apache.ojb.broker.util.configuration.impl.ConfigurationAbstractImpl; |
| 25 | |
import org.apache.commons.lang.SystemUtils; |
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
public class LoggingConfiguration extends ConfigurationAbstractImpl |
| 34 | |
{ |
| 35 | |
|
| 36 | |
|
| 37 | |
public static final String PROPERTY_COMMONS_LOGGING_LOG = "org.apache.commons.logging.Log"; |
| 38 | |
|
| 39 | |
|
| 40 | |
public static final String PROPERTY_COMMONS_LOGGING_LOGFACTORY = "org.apache.commons.logging.LogFactory"; |
| 41 | |
|
| 42 | |
public static final String PROPERTY_OJB_LOGGERCLASS = "org.apache.ojb.broker.util.logging.Logger.class"; |
| 43 | |
|
| 44 | |
public static final String PROPERTY_OJB_LOGGERCONFIGFILE = "org.apache.ojb.broker.util.logging.Logger.configFile"; |
| 45 | |
|
| 46 | |
public static final String OJB_LOGGING_PROPERTIES_FILE = "OJB-logging.properties"; |
| 47 | |
|
| 48 | |
public static final String OJB_DEFAULT_LOG_LEVEL = "WARN"; |
| 49 | |
|
| 50 | |
public static final String OJB_DEFAULT_BOOT_LOG_LEVEL = "INFO"; |
| 51 | |
|
| 52 | |
|
| 53 | |
private Class _loggerClass; |
| 54 | |
|
| 55 | |
private String _loggerConfigFile; |
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
public LoggingConfiguration() |
| 61 | |
{ |
| 62 | |
super(); |
| 63 | |
} |
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
protected void load() |
| 69 | |
{ |
| 70 | |
Logger bootLogger = LoggerFactory.getBootLogger(); |
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
ClassLoader contextLoader = ClassHelper.getClassLoader(); |
| 76 | |
String loggerClassName; |
| 77 | |
|
| 78 | |
_loggerClass = null; |
| 79 | |
properties = new Properties(); |
| 80 | |
loggerClassName = getLoggerClass(System.getProperties()); |
| 81 | |
_loggerConfigFile = getLoggerConfigFile(System.getProperties()); |
| 82 | |
|
| 83 | |
InputStream ojbLogPropFile; |
| 84 | |
if (loggerClassName == null) |
| 85 | |
{ |
| 86 | |
|
| 87 | |
String ojbLogPropFilePath = System.getProperty(OJB_LOGGING_PROPERTIES_FILE, OJB_LOGGING_PROPERTIES_FILE); |
| 88 | |
try |
| 89 | |
{ |
| 90 | |
URL ojbLoggingURL = ClassHelper.getResource(ojbLogPropFilePath); |
| 91 | |
if (ojbLoggingURL == null) |
| 92 | |
{ |
| 93 | |
ojbLoggingURL = (new File(ojbLogPropFilePath)).toURL(); |
| 94 | |
} |
| 95 | |
ojbLogPropFile = ojbLoggingURL.openStream(); |
| 96 | |
try |
| 97 | |
{ |
| 98 | |
bootLogger.info("Found logging properties file: " + ojbLogPropFilePath); |
| 99 | |
properties.load(ojbLogPropFile); |
| 100 | |
_loggerConfigFile = getLoggerConfigFile(properties); |
| 101 | |
loggerClassName = getLoggerClass(properties); |
| 102 | |
} |
| 103 | |
finally |
| 104 | |
{ |
| 105 | |
ojbLogPropFile.close(); |
| 106 | |
} |
| 107 | |
} |
| 108 | |
catch (Exception ex) |
| 109 | |
{ |
| 110 | |
if(loggerClassName == null) |
| 111 | |
{ |
| 112 | |
bootLogger.warn("Can't read logging properties file using path '" + ojbLogPropFilePath |
| 113 | |
+ "', message is: " + SystemUtils.LINE_SEPARATOR + ex.getMessage() |
| 114 | |
+ SystemUtils.LINE_SEPARATOR + "Will try to load logging properties from OJB.properties file"); |
| 115 | |
} |
| 116 | |
else |
| 117 | |
{ |
| 118 | |
bootLogger.info("Problems while closing resources for path '" + ojbLogPropFilePath |
| 119 | |
+ "', message is: " + SystemUtils.LINE_SEPARATOR + ex.getMessage(), ex); |
| 120 | |
} |
| 121 | |
} |
| 122 | |
} |
| 123 | |
if (loggerClassName == null) |
| 124 | |
{ |
| 125 | |
|
| 126 | |
|
| 127 | |
String ojbPropFile = System.getProperty("OJB.properties", "OJB.properties"); |
| 128 | |
|
| 129 | |
try |
| 130 | |
{ |
| 131 | |
ojbLogPropFile = contextLoader.getResourceAsStream(ojbPropFile); |
| 132 | |
if (ojbLogPropFile != null) |
| 133 | |
{ |
| 134 | |
try |
| 135 | |
{ |
| 136 | |
properties.load(ojbLogPropFile); |
| 137 | |
loggerClassName = getLoggerClass(properties); |
| 138 | |
_loggerConfigFile = getLoggerConfigFile(properties); |
| 139 | |
if (loggerClassName != null) |
| 140 | |
{ |
| 141 | |
|
| 142 | |
bootLogger.warn("Please use a separate '"+OJB_LOGGING_PROPERTIES_FILE+"' file to specify your logging settings"); |
| 143 | |
} |
| 144 | |
} |
| 145 | |
finally |
| 146 | |
{ |
| 147 | |
ojbLogPropFile.close(); |
| 148 | |
} |
| 149 | |
} |
| 150 | |
} |
| 151 | |
catch (Exception ex) |
| 152 | |
{} |
| 153 | |
} |
| 154 | |
if (loggerClassName != null) |
| 155 | |
{ |
| 156 | |
try |
| 157 | |
{ |
| 158 | |
_loggerClass = ClassHelper.getClass(loggerClassName); |
| 159 | |
bootLogger.info("Logging: Found logger class '" + loggerClassName); |
| 160 | |
} |
| 161 | |
catch (ClassNotFoundException ex) |
| 162 | |
{ |
| 163 | |
_loggerClass = PoorMansLoggerImpl.class; |
| 164 | |
bootLogger.warn("Could not load logger class "+loggerClassName+", defaulting to "+_loggerClass.getName(), ex); |
| 165 | |
} |
| 166 | |
} |
| 167 | |
else |
| 168 | |
{ |
| 169 | |
|
| 170 | |
if ((System.getProperty(PROPERTY_COMMONS_LOGGING_LOG) != null) || |
| 171 | |
(System.getProperty(PROPERTY_COMMONS_LOGGING_LOGFACTORY) != null)) |
| 172 | |
{ |
| 173 | |
|
| 174 | |
_loggerClass = CommonsLoggerImpl.class; |
| 175 | |
bootLogger.info("Logging: Found commons logging properties, use " + _loggerClass); |
| 176 | |
} |
| 177 | |
else |
| 178 | |
{ |
| 179 | |
|
| 180 | |
try |
| 181 | |
{ |
| 182 | |
ojbLogPropFile = contextLoader.getResourceAsStream("log4j.properties"); |
| 183 | |
if (ojbLogPropFile != null) |
| 184 | |
{ |
| 185 | |
|
| 186 | |
_loggerClass = Log4jLoggerImpl.class; |
| 187 | |
_loggerConfigFile = "log4j.properties"; |
| 188 | |
bootLogger.info("Logging: Found 'log4j.properties' file, use " + _loggerClass); |
| 189 | |
ojbLogPropFile.close(); |
| 190 | |
} |
| 191 | |
} |
| 192 | |
catch (Exception ex) |
| 193 | |
{} |
| 194 | |
if (_loggerClass == null) |
| 195 | |
{ |
| 196 | |
|
| 197 | |
try |
| 198 | |
{ |
| 199 | |
ojbLogPropFile = contextLoader.getResourceAsStream("commons-logging.properties"); |
| 200 | |
if (ojbLogPropFile != null) |
| 201 | |
{ |
| 202 | |
|
| 203 | |
_loggerClass = CommonsLoggerImpl.class; |
| 204 | |
_loggerConfigFile = "commons-logging.properties"; |
| 205 | |
bootLogger.info("Logging: Found 'commons-logging.properties' file, use " + _loggerClass); |
| 206 | |
ojbLogPropFile.close(); |
| 207 | |
} |
| 208 | |
} |
| 209 | |
catch (Exception ex) |
| 210 | |
{} |
| 211 | |
if (_loggerClass == null) |
| 212 | |
{ |
| 213 | |
|
| 214 | |
bootLogger.info("** Can't find logging configuration file, use default logger **"); |
| 215 | |
_loggerClass = PoorMansLoggerImpl.class; |
| 216 | |
} |
| 217 | |
} |
| 218 | |
} |
| 219 | |
} |
| 220 | |
} |
| 221 | |
|
| 222 | |
private String getLoggerClass(Properties props) |
| 223 | |
{ |
| 224 | |
String loggerClassName = props.getProperty(PROPERTY_OJB_LOGGERCLASS); |
| 225 | |
|
| 226 | |
if (loggerClassName == null) |
| 227 | |
{ |
| 228 | |
loggerClassName = props.getProperty("LoggerClass"); |
| 229 | |
} |
| 230 | |
return loggerClassName; |
| 231 | |
} |
| 232 | |
|
| 233 | |
private String getLoggerConfigFile(Properties props) |
| 234 | |
{ |
| 235 | |
String loggerConfigFile = props.getProperty(PROPERTY_OJB_LOGGERCONFIGFILE); |
| 236 | |
|
| 237 | |
if (loggerConfigFile == null) |
| 238 | |
{ |
| 239 | |
loggerConfigFile = props.getProperty("LoggerConfigFile"); |
| 240 | |
} |
| 241 | |
return loggerConfigFile; |
| 242 | |
} |
| 243 | |
|
| 244 | |
public String getLogLevel(String loggerName) |
| 245 | |
{ |
| 246 | |
|
| 247 | |
|
| 248 | |
|
| 249 | |
|
| 250 | |
|
| 251 | |
return getString(loggerName + ".LogLevel", getString("ROOT.LogLevel", OJB_DEFAULT_LOG_LEVEL)); |
| 252 | |
} |
| 253 | |
|
| 254 | |
|
| 255 | |
|
| 256 | |
|
| 257 | |
public void setLogger(Logger loggerInstance) |
| 258 | |
{ |
| 259 | |
|
| 260 | |
} |
| 261 | |
|
| 262 | |
|
| 263 | |
|
| 264 | |
|
| 265 | |
|
| 266 | |
|
| 267 | |
public Class getLoggerClass() |
| 268 | |
{ |
| 269 | |
return _loggerClass; |
| 270 | |
} |
| 271 | |
|
| 272 | |
|
| 273 | |
|
| 274 | |
|
| 275 | |
|
| 276 | |
|
| 277 | |
public String getLoggerConfigFile() |
| 278 | |
{ |
| 279 | |
return _loggerConfigFile; |
| 280 | |
} |
| 281 | |
} |