KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
20 import org.apache.ojb.broker.util.configuration.Configuration;
21 import org.apache.ojb.broker.util.configuration.ConfigurationException;
22
23 /**
24  * This is a Logger implementation based on jakarta commons logging.
25  * It can be enabled by putting
26  * LoggerClass=org.apache.ojb.broker.util.logging.CommonsLoggerImpl
27  * in the OJB .properties file. <br>
28  *
29  * @author <a HREF="mailto:jbraeuchi@hotmail.com">Jakob Braeuchi</a>
30  * @version $Id: CommonsLoggerImpl.java,v 1.4.2.3 2005/12/21 22:28:16 tomdz Exp $
31  */

32 public class CommonsLoggerImpl implements Logger
33 {
34     private String JavaDoc name;
35     private transient Log log;
36
37
38     /**
39      * Constructor for CommonsLoggerImpl.
40      */

41     public CommonsLoggerImpl(String JavaDoc aName)
42     {
43         this.name = aName;
44     }
45
46     /**
47      * Returns the log.
48      * @return Log
49      */

50     public Log getLog()
51     {
52         /*
53         Logger interface extends Serializable, thus Log field is
54         declared 'transient' and we have to null-check
55         */

56         if(log == null)
57         {
58             log = LogFactory.getLog(name);
59         }
60         return log;
61     }
62
63     /**
64      * @see org.apache.ojb.broker.util.logging.Logger#isEnabledFor(int)
65      */

66     public boolean isEnabledFor(int priority)
67     {
68         Log commonsLog = getLog();
69         switch(priority)
70         {
71             case Logger.DEBUG: return commonsLog.isDebugEnabled();
72             case Logger.INFO: return commonsLog.isInfoEnabled();
73             case Logger.WARN: return commonsLog.isWarnEnabled();
74             case Logger.ERROR: return commonsLog.isErrorEnabled();
75             case Logger.FATAL: return commonsLog.isFatalEnabled();
76         }
77         return false;
78     }
79
80     /**
81      * @see org.apache.ojb.broker.util.logging.Logger#debug(Object)
82      */

83     public void debug(Object JavaDoc pObject)
84     {
85         getLog().debug(pObject);
86     }
87
88     /**
89      * @see org.apache.ojb.broker.util.logging.Logger#info(Object)
90      */

91     public void info(Object JavaDoc pObject)
92     {
93         getLog().info(pObject);
94     }
95
96     /**
97      * @see org.apache.ojb.broker.util.logging.Logger#warn(Object)
98      */

99     public void warn(Object JavaDoc pObject)
100     {
101         getLog().warn(pObject);
102     }
103
104     /**
105      * @see org.apache.ojb.broker.util.logging.Logger#error(Object)
106      */

107     public void error(Object JavaDoc pObject)
108     {
109         getLog().error(pObject);
110     }
111
112     /**
113      * @see org.apache.ojb.broker.util.logging.Logger#fatal(Object)
114      */

115     public void fatal(Object JavaDoc pObject)
116     {
117         getLog().fatal(pObject);
118     }
119
120     /**
121      * @see org.apache.ojb.broker.util.logging.Logger#debug(Object, Throwable)
122      */

123     public void debug(Object JavaDoc message, Throwable JavaDoc obj)
124     {
125         getLog().debug(message, obj);
126     }
127
128     /**
129      * @see org.apache.ojb.broker.util.logging.Logger#info(Object, Throwable)
130      */

131     public void info(Object JavaDoc message, Throwable JavaDoc obj)
132     {
133         getLog().info(message, obj);
134     }
135
136     /**
137      * @see org.apache.ojb.broker.util.logging.Logger#warn(Object, Throwable)
138      */

139     public void warn(Object JavaDoc message, Throwable JavaDoc obj)
140     {
141         getLog().warn(message, obj);
142     }
143
144     /**
145      * @see org.apache.ojb.broker.util.logging.Logger#error(Object, Throwable)
146      */

147     public void error(Object JavaDoc message, Throwable JavaDoc obj)
148     {
149         getLog().error(message, obj);
150     }
151
152     /**
153      * @see org.apache.ojb.broker.util.logging.Logger#fatal(Object, Throwable)
154      */

155     public void fatal(Object JavaDoc message, Throwable JavaDoc obj)
156     {
157         getLog().fatal(message, obj);
158     }
159
160     /**
161      * @see org.apache.ojb.broker.util.logging.Logger#isDebugEnabled()
162      */

163     public boolean isDebugEnabled()
164     {
165         return getLog().isDebugEnabled();
166     }
167
168     /**
169      * @see org.apache.ojb.broker.util.logging.Logger#getName()
170      */

171     public String JavaDoc getName()
172     {
173         return name;
174     }
175
176     /**
177      * @see org.apache.ojb.broker.util.logging.Logger#safeDebug(String, Object)
178      */

179     public void safeDebug(String JavaDoc message, Object JavaDoc obj)
180     {
181         if (getLog().isDebugEnabled())
182         {
183             String JavaDoc toString = safeToString(obj);
184             getLog().debug(message + " : " + toString);
185         }
186     }
187
188     /**
189      * @see org.apache.ojb.broker.util.logging.Logger#safeDebug(String, Object, Throwable)
190      */

191     public void safeDebug(String JavaDoc message, Object JavaDoc obj, Throwable JavaDoc t)
192     {
193         if (getLog().isDebugEnabled())
194         {
195             String JavaDoc toString = safeToString(obj);
196             getLog().debug(message + " : " + toString, t);
197         }
198     }
199
200     /**
201      * @see org.apache.ojb.broker.util.logging.Logger#safeInfo(String, Object)
202      */

203     public void safeInfo(String JavaDoc message, Object JavaDoc obj)
204     {
205         if (getLog().isInfoEnabled())
206         {
207             String JavaDoc toString = safeToString(obj);
208             getLog().info(message + " : " + toString);
209         }
210     }
211
212     /**
213      * @see org.apache.ojb.broker.util.logging.Logger#safeInfo(String, Object, Throwable)
214      */

215     public void safeInfo(String JavaDoc message, Object JavaDoc obj, Throwable JavaDoc t)
216     {
217         if (getLog().isInfoEnabled())
218         {
219             String JavaDoc toString = safeToString(obj);
220             getLog().info(message + " : " + toString, t);
221         }
222     }
223
224     /**
225      * @see org.apache.ojb.broker.util.logging.Logger#safeWarn(String, Object)
226      */

227     public void safeWarn(String JavaDoc message, Object JavaDoc obj)
228     {
229         if (getLog().isWarnEnabled())
230         {
231             String JavaDoc toString = safeToString(obj);
232             getLog().warn(message + " : " + toString);
233         }
234     }
235
236     /**
237      * @see org.apache.ojb.broker.util.logging.Logger#safeWarn(String, Object, Throwable)
238      */

239     public void safeWarn(String JavaDoc message, Object JavaDoc obj, Throwable JavaDoc t)
240     {
241         if (getLog().isWarnEnabled())
242         {
243             String JavaDoc toString = safeToString(obj);
244             getLog().warn(message + " : " + toString, t);
245         }
246     }
247
248     /**
249      * @see org.apache.ojb.broker.util.logging.Logger#safeError(String, Object)
250      */

251     public void safeError(String JavaDoc message, Object JavaDoc obj)
252     {
253         if (getLog().isErrorEnabled())
254         {
255             String JavaDoc toString = safeToString(obj);
256             getLog().error(message + " : " + toString);
257         }
258     }
259
260     /**
261      * @see org.apache.ojb.broker.util.logging.Logger#safeError(String, Object, Throwable)
262      */

263     public void safeError(String JavaDoc message, Object JavaDoc obj, Throwable JavaDoc t)
264     {
265         if (getLog().isErrorEnabled())
266         {
267             String JavaDoc toString = safeToString(obj);
268             getLog().error(message + " : " + toString, t);
269         }
270     }
271
272     /**
273      * @see org.apache.ojb.broker.util.logging.Logger#safeFatal(String, Object)
274      */

275     public void safeFatal(String JavaDoc message, Object JavaDoc obj)
276     {
277         if (getLog().isFatalEnabled())
278         {
279             String JavaDoc toString = safeToString(obj);
280             getLog().fatal(message + " : " + toString);
281         }
282     }
283
284     /**
285      * @see org.apache.ojb.broker.util.logging.Logger#safeFatal(String, Object, Throwable)
286      */

287     public void safeFatal(String JavaDoc message, Object JavaDoc obj, Throwable JavaDoc t)
288     {
289         if (getLog().isFatalEnabled())
290         {
291             String JavaDoc toString = safeToString(obj);
292             getLog().fatal(message + " : " + toString, t);
293         }
294     }
295
296     /**
297      * provides a safe toString
298      */

299     private String JavaDoc safeToString(Object JavaDoc obj)
300     {
301         String JavaDoc toString = null;
302         if (obj != null)
303         {
304             try
305             {
306                 toString = obj.toString();
307             }
308             catch (Throwable JavaDoc ex)
309             {
310                 toString = "BAD toString() impl for " + obj.getClass().getName();
311             }
312         }
313         return toString;
314     }
315
316     /**
317      * @see org.apache.ojb.broker.util.configuration.Configurable#configure(Configuration)
318      */

319     public void configure(Configuration config) throws ConfigurationException
320     {
321         // do nothing
322
}
323 }
324
Popular Tags