KickJava   Java API By Example, From Geeks To Geeks.

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


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.net.URL JavaDoc;
19 import java.util.Enumeration JavaDoc;
20
21 import org.apache.log4j.LogManager;
22 import org.apache.log4j.Level;
23 import org.apache.log4j.PropertyConfigurator;
24 import org.apache.ojb.broker.util.ClassHelper;
25 import org.apache.ojb.broker.util.configuration.Configuration;
26 import org.apache.ojb.broker.util.configuration.ConfigurationException;
27
28 /**
29  * This is a Logger implementation based on Log4j.
30  * It can be enabled by putting
31  * LoggerClass=org.apache.ojb.broker.util.logging.Log4jLoggerImpl
32  * in the OJB .properties file. <br>
33  * If you want log4j to initialize from a property file you can add
34  * LoggerConfigFile=log4j.properties to the org.apache.ojb.properties file.
35  * the logger only initializes log4j if the application hasn't done it yet
36  *
37  * You can find sample log4j.properties file in the log4j web site
38  * http://jakarta.apache.org/log4j
39  * in the javadoc look for org.apache.log4j.examples
40  *
41  * @author Bertrand
42  * @author Thomas Mahler
43  * @version $Id: Log4jLoggerImpl.java,v 1.16.2.4 2005/12/21 22:28:16 tomdz Exp $
44  */

45 public class Log4jLoggerImpl implements Logger
46 {
47     static private final String JavaDoc FQCN = Log4jLoggerImpl.class.getName();
48     /** flag about log4j configuration state */
49     private static boolean log4jConfigured = false;
50
51     private transient org.apache.log4j.Logger logger;
52     private String JavaDoc name;
53
54     /** Helper method to check if log4j is already configured */
55     private static synchronized boolean isLog4JConfigured()
56     {
57         if(!log4jConfigured)
58         {
59             Enumeration JavaDoc en = org.apache.log4j.Logger.getRootLogger().getAllAppenders();
60
61             if (!(en instanceof org.apache.log4j.helpers.NullEnumeration))
62             {
63                 log4jConfigured = true;
64             }
65             else
66             {
67                 Enumeration JavaDoc cats = LogManager.getCurrentLoggers();
68                 while (cats.hasMoreElements())
69                 {
70                     org.apache.log4j.Logger c = (org.apache.log4j.Logger) cats.nextElement();
71                     if (!(c.getAllAppenders() instanceof org.apache.log4j.helpers.NullEnumeration))
72                     {
73                         log4jConfigured = true;
74                     }
75                 }
76             }
77             if(log4jConfigured)
78             {
79                 String JavaDoc msg = "Log4J is already configured, will not search for log4j properties file";
80                 LoggerFactory.getBootLogger().info(msg);
81             }
82             else
83             {
84                 LoggerFactory.getBootLogger().info("Log4J is not configured");
85             }
86         }
87         return log4jConfigured;
88     }
89
90     /**
91      * Initialization of log4j <br>
92      * <b>NOTE</b> - if log4j property file is called log4j.properties then
93      * log4j will be configured already.
94      */

95     private static synchronized void initializeLog4JSubSystem(String JavaDoc configFile)
96     {
97         LoggerFactory.getBootLogger().info("Initializing Log4J using file: '" + configFile + "'");
98         if(configFile == null || "".equals(configFile.trim()))
99         {
100             // no configuration available
101
LoggerFactory.getBootLogger().warn("No log4j configuration file specified");
102         }
103         else
104         {
105             // try resource look in classpath
106
URL JavaDoc url = ClassHelper.getResource(configFile);
107             LoggerFactory.getBootLogger().info("Initializing Log4J : resource from config file:" + url);
108             if (url != null)
109             {
110                 PropertyConfigurator.configure(url);
111             }
112             // if file is not in classpath try ordinary filesystem lookup
113
else
114             {
115                 PropertyConfigurator.configure(configFile);
116             }
117         }
118         log4jConfigured = true;
119     }
120
121     public Log4jLoggerImpl(String JavaDoc name)
122     {
123         this.name = name;
124     }
125
126     /**
127      * @see org.apache.ojb.broker.util.configuration.Configurable#configure(Configuration)
128      * This method must be performed by LogFactory after creating a logger instance.
129      */

130     public void configure(Configuration config) throws ConfigurationException
131     {
132         if (!isLog4JConfigured())
133         {
134             LoggingConfiguration lc = (LoggingConfiguration) config;
135             initializeLog4JSubSystem(lc.getLoggerConfigFile());
136         }
137     }
138
139     /**
140      * Gets the logger.
141      *
142      * @return Returns a Category
143      */

144     private org.apache.log4j.Logger getLogger()
145     {
146         /*
147         Logger interface extends Serializable, thus Log field is
148         declared 'transient' and we have to null-check
149         */

150         if (logger == null)
151         {
152             logger = org.apache.log4j.Logger.getLogger(name);
153         }
154         return logger;
155     }
156
157     public String JavaDoc getName()
158     {
159         return name;
160     }
161
162     private Level getLevel()
163     {
164         return getLogger().getEffectiveLevel();
165     }
166
167     /**
168      * generate a message for loglevel DEBUG
169      *
170      * @param pObject the message Object
171      */

172     public final void debug(Object JavaDoc pObject)
173     {
174         getLogger().log(FQCN, Level.DEBUG, pObject, null);
175     }
176
177     /**
178      * generate a message for loglevel INFO
179      *
180      * @param pObject the message Object
181      */

182     public final void info(Object JavaDoc pObject)
183     {
184         getLogger().log(FQCN, Level.INFO, pObject, null);
185     }
186
187     /**
188      * generate a message for loglevel WARN
189      *
190      * @param pObject the message Object
191      */

192     public final void warn(Object JavaDoc pObject)
193     {
194         getLogger().log(FQCN, Level.WARN, pObject, null);
195     }
196
197     /**
198      * generate a message for loglevel ERROR
199      *
200      * @param pObject the message Object
201      */

202     public final void error(Object JavaDoc pObject)
203     {
204         getLogger().log(FQCN, Level.ERROR, pObject, null);
205     }
206
207     /**
208      * generate a message for loglevel FATAL
209      *
210      * @param pObject the message Object
211      */

212     public final void fatal(Object JavaDoc pObject)
213     {
214         getLogger().log(FQCN, Level.FATAL, pObject, null);
215     }
216
217     public void debug(Object JavaDoc message, Throwable JavaDoc obj)
218     {
219         getLogger().log(FQCN, Level.DEBUG, message, obj);
220     }
221
222     public void error(Object JavaDoc message, Throwable JavaDoc obj)
223     {
224         getLogger().log(FQCN, Level.ERROR, message, obj);
225     }
226
227     public void fatal(Object JavaDoc message, Throwable JavaDoc obj)
228     {
229         getLogger().log(FQCN, Level.FATAL, message, obj);
230     }
231
232     public void info(Object JavaDoc message, Throwable JavaDoc obj)
233     {
234         getLogger().log(FQCN, Level.INFO, message, obj);
235     }
236
237     public void warn(Object JavaDoc message, Throwable JavaDoc obj)
238     {
239         getLogger().log(FQCN, Level.WARN, message, obj);
240     }
241
242     public void safeDebug(String JavaDoc message, Object JavaDoc obj)
243     {
244         if (Level.DEBUG.isGreaterOrEqual(getLevel()))
245         {
246             String JavaDoc toString = null;
247             if (obj != null)
248             {
249                 try
250                 {
251                     toString = obj.toString();
252                 }
253                 catch (Throwable JavaDoc t)
254                 {
255                     toString = "BAD toString() impl for " + obj.getClass().getName();
256                 }
257             }
258             debug(message + " : " + toString);
259         }
260     }
261
262     public void safeDebug(String JavaDoc message, Object JavaDoc obj, Throwable JavaDoc throwable)
263     {
264         if (Level.DEBUG.isGreaterOrEqual(getLevel()))
265         {
266             String JavaDoc toString = null;
267             if (obj != null)
268             {
269                 try
270                 {
271                     toString = obj.toString();
272                 }
273                 catch (Throwable JavaDoc t)
274                 {
275                     toString = "BAD toString() impl for " + obj.getClass().getName();
276                 }
277             }
278             debug(message + " : " + toString, throwable);
279         }
280     }
281
282     public void safeInfo(String JavaDoc message, Object JavaDoc obj)
283     {
284         if (Level.INFO.isGreaterOrEqual(getLevel()))
285         {
286             String JavaDoc toString = null;
287             if (obj != null)
288             {
289                 try
290                 {
291                     toString = obj.toString();
292                 }
293                 catch (Throwable JavaDoc t)
294                 {
295                     toString = "BAD toString() impl for " + obj.getClass().getName();
296                 }
297             }
298             info(message + " : " + toString);
299         }
300     }
301
302     public void safeInfo(String JavaDoc message, Object JavaDoc obj, Throwable JavaDoc throwable)
303     {
304         if (Level.INFO.isGreaterOrEqual(getLevel()))
305         {
306             String JavaDoc toString = null;
307             if (obj != null)
308             {
309                 try
310                 {
311                     toString = obj.toString();
312                 }
313                 catch (Throwable JavaDoc t)
314                 {
315                     toString = "BAD toString() impl for " + obj.getClass().getName();
316                 }
317             }
318             info(message + " : " + toString, throwable);
319         }
320     }
321
322     public void safeWarn(String JavaDoc message, Object JavaDoc obj)
323     {
324         if (Level.WARN.isGreaterOrEqual(getLevel()))
325         {
326             String JavaDoc toString;
327             try
328             {
329                 toString = obj.toString();
330             }
331             catch (Throwable JavaDoc t)
332             {
333                 toString = "BAD toString() impl for " + obj.getClass().getName();
334             }
335              warn(message + " : " + toString);
336         }
337     }
338
339     public void safeWarn(String JavaDoc message, Object JavaDoc obj, Throwable JavaDoc throwable)
340     {
341         if (Level.WARN.isGreaterOrEqual(getLevel()))
342         {
343             String JavaDoc toString;
344             try
345             {
346                 toString = obj.toString();
347             }
348             catch (Throwable JavaDoc t)
349             {
350                 toString = "BAD toString() impl for " + obj.getClass().getName();
351             }
352             warn(message + " : " + toString, throwable);
353         }
354     }
355
356     public void safeError(String JavaDoc message, Object JavaDoc obj)
357     {
358         if (Level.ERROR.isGreaterOrEqual(getLevel()))
359         {
360             String JavaDoc toString;
361             try
362             {
363                 toString = obj.toString();
364             }
365             catch (Throwable JavaDoc t)
366             {
367                 toString = "BAD toString() impl for " + obj.getClass().getName();
368             }
369             error(message + " : " + toString);
370         }
371     }
372
373     public void safeError(String JavaDoc message, Object JavaDoc obj, Throwable JavaDoc throwable)
374     {
375         if (Level.ERROR.isGreaterOrEqual(getLevel()))
376         {
377             String JavaDoc toString;
378             try
379             {
380                 toString = obj.toString();
381             }
382             catch (Throwable JavaDoc t)
383             {
384                 toString = "BAD toString() impl for " + obj.getClass().getName();
385             }
386             error(message + " : " + toString, throwable);
387         }
388     }
389
390     public void safeFatal(String JavaDoc message, Object JavaDoc obj)
391     {
392         if (Level.FATAL.isGreaterOrEqual(getLevel()))
393         {
394             String JavaDoc toString;
395             try
396             {
397                 toString = obj.toString();
398             }
399             catch (Throwable JavaDoc t)
400             {
401                 toString = "BAD toString() impl for " + obj.getClass().getName();
402             }
403             fatal(message + " : " + toString);
404         }
405     }
406
407     public void safeFatal(String JavaDoc message, Object JavaDoc obj, Throwable JavaDoc throwable)
408     {
409         if (Level.FATAL.isGreaterOrEqual(getLevel()))
410         {
411             String JavaDoc toString;
412             try
413             {
414                 toString = obj.toString();
415             }
416             catch (Throwable JavaDoc t)
417             {
418                 toString = "BAD toString() impl for " + obj.getClass().getName();
419             }
420             fatal(message + " : " + toString, throwable);
421         }
422     }
423
424     public boolean isDebugEnabled()
425     {
426         return getLogger().isDebugEnabled();
427     }
428
429     public boolean isEnabledFor(int priority)
430     {
431         org.apache.log4j.Logger log4j = getLogger();
432         switch(priority)
433         {
434             case Logger.DEBUG: return log4j.isDebugEnabled();
435             case Logger.INFO: return log4j.isInfoEnabled();
436             case Logger.WARN: return log4j.isEnabledFor(org.apache.log4j.Priority.WARN);
437             case Logger.ERROR: return log4j.isEnabledFor(org.apache.log4j.Priority.ERROR);
438             case Logger.FATAL: return log4j.isEnabledFor(org.apache.log4j.Priority.FATAL);
439         }
440         return false;
441     }
442 }
443
Popular Tags