001: package org.apache.ojb.broker.util.logging;
002:
003: /* Copyright 2002-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import java.io.InputStream;
019: import java.io.File;
020: import java.util.Properties;
021: import java.net.URL;
022:
023: import org.apache.ojb.broker.util.ClassHelper;
024: import org.apache.ojb.broker.util.configuration.impl.ConfigurationAbstractImpl;
025: import org.apache.commons.lang.SystemUtils;
026:
027: /**
028: * Provides the configuration for the logging. Note that this is separated from the OJB
029: * configuration.
030: *
031: * @version $Id: LoggingConfiguration.java,v 1.8.2.4 2005/12/21 22:28:16 tomdz Exp $
032: */
033: public class LoggingConfiguration extends ConfigurationAbstractImpl {
034: /** The commons-logging property denoting which log to use. This property
035: * is repeated here to avoid making this class dependent upon commons-logging */
036: public static final String PROPERTY_COMMONS_LOGGING_LOG = "org.apache.commons.logging.Log";
037: /** The commons-logging property denoting which log factory to use. This property
038: * is repeated here to avoid making this class dependent upon commons-logging */
039: public static final String PROPERTY_COMMONS_LOGGING_LOGFACTORY = "org.apache.commons.logging.LogFactory";
040: /** The property denoting the OJB logger class */
041: public static final String PROPERTY_OJB_LOGGERCLASS = "org.apache.ojb.broker.util.logging.Logger.class";
042: /** The property denoting the config file for the OJB logger class */
043: public static final String PROPERTY_OJB_LOGGERCONFIGFILE = "org.apache.ojb.broker.util.logging.Logger.configFile";
044: /** Default filename of the OJB logging properties file */
045: public static final String OJB_LOGGING_PROPERTIES_FILE = "OJB-logging.properties";
046: /** Default log level */
047: public static final String OJB_DEFAULT_LOG_LEVEL = "WARN";
048: /** Default boot log level */
049: public static final String OJB_DEFAULT_BOOT_LOG_LEVEL = "INFO";
050:
051: /** The logger class */
052: private Class _loggerClass;
053: /** The config file for the logger */
054: private String _loggerConfigFile;
055:
056: /**
057: * Creates a new logging configuration object which automatically initializes itself.
058: */
059: public LoggingConfiguration() {
060: super ();
061: }
062:
063: /* (non-Javadoc)
064: * @see org.apache.ojb.broker.util.configuration.impl.ConfigurationAbstractImpl#load()
065: */
066: protected void load() {
067: Logger bootLogger = LoggerFactory.getBootLogger();
068:
069: // first we check whether the system property
070: // org.apache.ojb.broker.util.logging.Logger
071: // is set (or its alias LoggerClass which is deprecated)
072: ClassLoader contextLoader = ClassHelper.getClassLoader();
073: String loggerClassName;
074:
075: _loggerClass = null;
076: properties = new Properties();
077: loggerClassName = getLoggerClass(System.getProperties());
078: _loggerConfigFile = getLoggerConfigFile(System.getProperties());
079:
080: InputStream ojbLogPropFile;
081: if (loggerClassName == null) {
082: // now we're trying to load the OJB-logging.properties file
083: String ojbLogPropFilePath = System.getProperty(
084: OJB_LOGGING_PROPERTIES_FILE,
085: OJB_LOGGING_PROPERTIES_FILE);
086: try {
087: URL ojbLoggingURL = ClassHelper
088: .getResource(ojbLogPropFilePath);
089: if (ojbLoggingURL == null) {
090: ojbLoggingURL = (new File(ojbLogPropFilePath))
091: .toURL();
092: }
093: ojbLogPropFile = ojbLoggingURL.openStream();
094: try {
095: bootLogger.info("Found logging properties file: "
096: + ojbLogPropFilePath);
097: properties.load(ojbLogPropFile);
098: _loggerConfigFile = getLoggerConfigFile(properties);
099: loggerClassName = getLoggerClass(properties);
100: } finally {
101: ojbLogPropFile.close();
102: }
103: } catch (Exception ex) {
104: if (loggerClassName == null) {
105: bootLogger
106: .warn("Can't read logging properties file using path '"
107: + ojbLogPropFilePath
108: + "', message is: "
109: + SystemUtils.LINE_SEPARATOR
110: + ex.getMessage()
111: + SystemUtils.LINE_SEPARATOR
112: + "Will try to load logging properties from OJB.properties file");
113: } else {
114: bootLogger.info(
115: "Problems while closing resources for path '"
116: + ojbLogPropFilePath
117: + "', message is: "
118: + SystemUtils.LINE_SEPARATOR
119: + ex.getMessage(), ex);
120: }
121: }
122: }
123: if (loggerClassName == null) {
124: // deprecated: load the OJB.properties file
125: // this is not good because we have all OJB properties in this config
126: String ojbPropFile = System.getProperty("OJB.properties",
127: "OJB.properties");
128:
129: try {
130: ojbLogPropFile = contextLoader
131: .getResourceAsStream(ojbPropFile);
132: if (ojbLogPropFile != null) {
133: try {
134: properties.load(ojbLogPropFile);
135: loggerClassName = getLoggerClass(properties);
136: _loggerConfigFile = getLoggerConfigFile(properties);
137: if (loggerClassName != null) {
138: // deprecation warning for after 1.0
139: bootLogger
140: .warn("Please use a separate '"
141: + OJB_LOGGING_PROPERTIES_FILE
142: + "' file to specify your logging settings");
143: }
144: } finally {
145: ojbLogPropFile.close();
146: }
147: }
148: } catch (Exception ex) {
149: }
150: }
151: if (loggerClassName != null) {
152: try {
153: _loggerClass = ClassHelper.getClass(loggerClassName);
154: bootLogger.info("Logging: Found logger class '"
155: + loggerClassName);
156: } catch (ClassNotFoundException ex) {
157: _loggerClass = PoorMansLoggerImpl.class;
158: bootLogger.warn("Could not load logger class "
159: + loggerClassName + ", defaulting to "
160: + _loggerClass.getName(), ex);
161: }
162: } else {
163: // still no logger configured - lets check whether commons-logging is configured
164: if ((System.getProperty(PROPERTY_COMMONS_LOGGING_LOG) != null)
165: || (System
166: .getProperty(PROPERTY_COMMONS_LOGGING_LOGFACTORY) != null)) {
167: // yep, so use commons-logging
168: _loggerClass = CommonsLoggerImpl.class;
169: bootLogger
170: .info("Logging: Found commons logging properties, use "
171: + _loggerClass);
172: } else {
173: // but perhaps there is a log4j.properties file ?
174: try {
175: ojbLogPropFile = contextLoader
176: .getResourceAsStream("log4j.properties");
177: if (ojbLogPropFile != null) {
178: // yep, so use log4j
179: _loggerClass = Log4jLoggerImpl.class;
180: _loggerConfigFile = "log4j.properties";
181: bootLogger
182: .info("Logging: Found 'log4j.properties' file, use "
183: + _loggerClass);
184: ojbLogPropFile.close();
185: }
186: } catch (Exception ex) {
187: }
188: if (_loggerClass == null) {
189: // or a commons-logging.properties file ?
190: try {
191: ojbLogPropFile = contextLoader
192: .getResourceAsStream("commons-logging.properties");
193: if (ojbLogPropFile != null) {
194: // yep, so use commons-logging
195: _loggerClass = CommonsLoggerImpl.class;
196: _loggerConfigFile = "commons-logging.properties";
197: bootLogger
198: .info("Logging: Found 'commons-logging.properties' file, use "
199: + _loggerClass);
200: ojbLogPropFile.close();
201: }
202: } catch (Exception ex) {
203: }
204: if (_loggerClass == null) {
205: // no, so default to poor man's logging
206: bootLogger
207: .info("** Can't find logging configuration file, use default logger **");
208: _loggerClass = PoorMansLoggerImpl.class;
209: }
210: }
211: }
212: }
213: }
214:
215: private String getLoggerClass(Properties props) {
216: String loggerClassName = props
217: .getProperty(PROPERTY_OJB_LOGGERCLASS);
218:
219: if (loggerClassName == null) {
220: loggerClassName = props.getProperty("LoggerClass");
221: }
222: return loggerClassName;
223: }
224:
225: private String getLoggerConfigFile(Properties props) {
226: String loggerConfigFile = props
227: .getProperty(PROPERTY_OJB_LOGGERCONFIGFILE);
228:
229: if (loggerConfigFile == null) {
230: loggerConfigFile = props.getProperty("LoggerConfigFile");
231: }
232: return loggerConfigFile;
233: }
234:
235: public String getLogLevel(String loggerName) {
236: /*
237: arminw:
238: use ROOT.LogLevel property to define global
239: default log level
240: */
241: return getString(loggerName + ".LogLevel", getString(
242: "ROOT.LogLevel", OJB_DEFAULT_LOG_LEVEL));
243: }
244:
245: /* (non-Javadoc)
246: * @see org.apache.ojb.broker.util.configuration.Configuration#setLogger(org.apache.ojb.broker.util.logging.Logger)
247: */
248: public void setLogger(Logger loggerInstance) {
249: // ignored - only logging via the boot logger
250: }
251:
252: /**
253: * Returns the logger class.
254: *
255: * @return The logger class
256: */
257: public Class getLoggerClass() {
258: return _loggerClass;
259: }
260:
261: /**
262: * Returns the name of the config file for the logger.
263: *
264: * @return The config file if it was configured
265: */
266: public String getLoggerConfigFile() {
267: return _loggerConfigFile;
268: }
269: }
|