KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > util > logging > LoggingConfiguration


1 package org.apache.ojb.broker.util.logging;
2
3 /* Copyright 2002-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 import java.io.InputStream JavaDoc;
19 import java.io.File JavaDoc;
20 import java.util.Properties JavaDoc;
21 import java.net.URL JavaDoc;
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  * Provides the configuration for the logging. Note that this is separated from the OJB
29  * configuration.
30  *
31  * @version $Id: LoggingConfiguration.java,v 1.8.2.4 2005/12/21 22:28:16 tomdz Exp $
32  */

33 public class LoggingConfiguration extends ConfigurationAbstractImpl
34 {
35     /** The commons-logging property denoting which log to use. This property
36      * is repeated here to avoid making this class dependent upon commons-logging */

37     public static final String JavaDoc PROPERTY_COMMONS_LOGGING_LOG = "org.apache.commons.logging.Log";
38     /** The commons-logging property denoting which log factory to use. This property
39      * is repeated here to avoid making this class dependent upon commons-logging */

40     public static final String JavaDoc PROPERTY_COMMONS_LOGGING_LOGFACTORY = "org.apache.commons.logging.LogFactory";
41     /** The property denoting the OJB logger class */
42     public static final String JavaDoc PROPERTY_OJB_LOGGERCLASS = "org.apache.ojb.broker.util.logging.Logger.class";
43     /** The property denoting the config file for the OJB logger class */
44     public static final String JavaDoc PROPERTY_OJB_LOGGERCONFIGFILE = "org.apache.ojb.broker.util.logging.Logger.configFile";
45     /** Default filename of the OJB logging properties file */
46     public static final String JavaDoc OJB_LOGGING_PROPERTIES_FILE = "OJB-logging.properties";
47     /** Default log level */
48     public static final String JavaDoc OJB_DEFAULT_LOG_LEVEL = "WARN";
49     /** Default boot log level */
50     public static final String JavaDoc OJB_DEFAULT_BOOT_LOG_LEVEL = "INFO";
51
52     /** The logger class */
53     private Class JavaDoc _loggerClass;
54     /** The config file for the logger */
55     private String JavaDoc _loggerConfigFile;
56
57     /**
58      * Creates a new logging configuration object which automatically initializes itself.
59      */

60     public LoggingConfiguration()
61     {
62         super();
63     }
64
65     /* (non-Javadoc)
66      * @see org.apache.ojb.broker.util.configuration.impl.ConfigurationAbstractImpl#load()
67      */

68     protected void load()
69     {
70         Logger bootLogger = LoggerFactory.getBootLogger();
71
72         // first we check whether the system property
73
// org.apache.ojb.broker.util.logging.Logger
74
// is set (or its alias LoggerClass which is deprecated)
75
ClassLoader JavaDoc contextLoader = ClassHelper.getClassLoader();
76         String JavaDoc loggerClassName;
77
78         _loggerClass = null;
79         properties = new Properties JavaDoc();
80         loggerClassName = getLoggerClass(System.getProperties());
81         _loggerConfigFile = getLoggerConfigFile(System.getProperties());
82
83         InputStream JavaDoc ojbLogPropFile;
84         if (loggerClassName == null)
85         {
86             // now we're trying to load the OJB-logging.properties file
87
String JavaDoc ojbLogPropFilePath = System.getProperty(OJB_LOGGING_PROPERTIES_FILE, OJB_LOGGING_PROPERTIES_FILE);
88             try
89             {
90                 URL JavaDoc ojbLoggingURL = ClassHelper.getResource(ojbLogPropFilePath);
91                 if (ojbLoggingURL == null)
92                 {
93                     ojbLoggingURL = (new File JavaDoc(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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc getLoggerClass(Properties JavaDoc props)
223     {
224         String JavaDoc 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 JavaDoc getLoggerConfigFile(Properties JavaDoc props)
234     {
235         String JavaDoc 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 JavaDoc getLogLevel(String JavaDoc 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 JavaDoc 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 JavaDoc getLoggerConfigFile()
278     {
279         return _loggerConfigFile;
280     }
281 }
282
Popular Tags