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.1 2007-08-24 22:17:32 ewestfal Exp $
032 */
033 public class LoggingConfiguration extends ConfigurationAbstractImpl
034 {
035 /** The commons-logging property denoting which log to use. This property
036 * is repeated here to avoid making this class dependent upon commons-logging */
037 public static final String PROPERTY_COMMONS_LOGGING_LOG = "org.apache.commons.logging.Log";
038 /** The commons-logging property denoting which log factory to use. This property
039 * is repeated here to avoid making this class dependent upon commons-logging */
040 public static final String PROPERTY_COMMONS_LOGGING_LOGFACTORY = "org.apache.commons.logging.LogFactory";
041 /** The property denoting the OJB logger class */
042 public static final String PROPERTY_OJB_LOGGERCLASS = "org.apache.ojb.broker.util.logging.Logger.class";
043 /** The property denoting the config file for the OJB logger class */
044 public static final String PROPERTY_OJB_LOGGERCONFIGFILE = "org.apache.ojb.broker.util.logging.Logger.configFile";
045 /** Default filename of the OJB logging properties file */
046 public static final String OJB_LOGGING_PROPERTIES_FILE = "OJB-logging.properties";
047 /** Default log level */
048 public static final String OJB_DEFAULT_LOG_LEVEL = "WARN";
049 /** Default boot log level */
050 public static final String OJB_DEFAULT_BOOT_LOG_LEVEL = "INFO";
051
052 /** The logger class */
053 private Class _loggerClass;
054 /** The config file for the logger */
055 private String _loggerConfigFile;
056
057 /**
058 * Creates a new logging configuration object which automatically initializes itself.
059 */
060 public LoggingConfiguration()
061 {
062 super();
063 }
064
065 /* (non-Javadoc)
066 * @see org.apache.ojb.broker.util.configuration.impl.ConfigurationAbstractImpl#load()
067 */
068 protected void load()
069 {
070 Logger bootLogger = LoggerFactory.getBootLogger();
071
072 // first we check whether the system property
073 // org.apache.ojb.broker.util.logging.Logger
074 // is set (or its alias LoggerClass which is deprecated)
075 ClassLoader contextLoader = ClassHelper.getClassLoader();
076 String loggerClassName;
077
078 _loggerClass = null;
079 properties = new Properties();
080 loggerClassName = getLoggerClass(System.getProperties());
081 _loggerConfigFile = getLoggerConfigFile(System.getProperties());
082
083 InputStream ojbLogPropFile;
084 if (loggerClassName == null)
085 {
086 // now we're trying to load the OJB-logging.properties file
087 String ojbLogPropFilePath = System.getProperty(OJB_LOGGING_PROPERTIES_FILE, OJB_LOGGING_PROPERTIES_FILE);
088 try
089 {
090 URL ojbLoggingURL = ClassHelper.getResource(ojbLogPropFilePath);
091 if (ojbLoggingURL == null)
092 {
093 ojbLoggingURL = (new File(ojbLogPropFilePath)).toURL();
094 }
095 ojbLogPropFile = ojbLoggingURL.openStream();
096 try
097 {
098 bootLogger.info("Found logging properties file: " + ojbLogPropFilePath);
099 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 // deprecated: load the OJB.properties file
126 // this is not good because we have all OJB properties in this config
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 // deprecation warning for after 1.0
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 // still no logger configured - lets check whether commons-logging is configured
170 if ((System.getProperty(PROPERTY_COMMONS_LOGGING_LOG) != null) ||
171 (System.getProperty(PROPERTY_COMMONS_LOGGING_LOGFACTORY) != null))
172 {
173 // yep, so use commons-logging
174 _loggerClass = CommonsLoggerImpl.class;
175 bootLogger.info("Logging: Found commons logging properties, use " + _loggerClass);
176 }
177 else
178 {
179 // but perhaps there is a log4j.properties file ?
180 try
181 {
182 ojbLogPropFile = contextLoader.getResourceAsStream("log4j.properties");
183 if (ojbLogPropFile != null)
184 {
185 // yep, so use log4j
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 // or a commons-logging.properties file ?
197 try
198 {
199 ojbLogPropFile = contextLoader.getResourceAsStream("commons-logging.properties");
200 if (ojbLogPropFile != null)
201 {
202 // yep, so use commons-logging
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 // no, so default to poor man's logging
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 arminw:
248 use ROOT.LogLevel property to define global
249 default log level
250 */
251 return getString(loggerName + ".LogLevel", getString("ROOT.LogLevel", OJB_DEFAULT_LOG_LEVEL));
252 }
253
254 /* (non-Javadoc)
255 * @see org.apache.ojb.broker.util.configuration.Configuration#setLogger(org.apache.ojb.broker.util.logging.Logger)
256 */
257 public void setLogger(Logger loggerInstance)
258 {
259 // ignored - only logging via the boot logger
260 }
261
262 /**
263 * Returns the logger class.
264 *
265 * @return The logger class
266 */
267 public Class getLoggerClass()
268 {
269 return _loggerClass;
270 }
271
272 /**
273 * Returns the name of the config file for the logger.
274 *
275 * @return The config file if it was configured
276 */
277 public String getLoggerConfigFile()
278 {
279 return _loggerConfigFile;
280 }
281 }