KickJava   Java API By Example, From Geeks To Geeks.

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


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.ojb.broker.util.configuration.Configuration;
19 import org.apache.ojb.broker.util.configuration.ConfigurationException;
20
21 /**
22  * this is a most simple Logger implementation.
23  * All output is directed to System.out.
24  *
25  * @author Thomas Mahler
26  * @version $Id: PoorMansLoggerImpl.java,v 1.11.2.3 2005/12/21 22:28:16 tomdz Exp $
27  */

28 public class PoorMansLoggerImpl implements Logger
29 {
30     protected static final String JavaDoc STR_DEBUG = "DEBUG";
31     protected static final String JavaDoc STR_INFO = "INFO";
32     protected static final String JavaDoc STR_WARN = "WARN";
33     protected static final String JavaDoc STR_ERROR = "ERROR";
34     protected static final String JavaDoc STR_FATAL = "FATAL";
35
36     protected static final String JavaDoc STR_DEBUG_MSG = "DEBUG: ";
37     protected static final String JavaDoc STR_INFO_MSG = "INFO: ";
38     protected static final String JavaDoc STR_WARN_MSG = "WARN: ";
39     protected static final String JavaDoc STR_ERROR_MSG = "ERROR: ";
40     protected static final String JavaDoc STR_FATAL_MSG = "FATAL: ";
41
42     protected static final String JavaDoc BRAKE_OPEN = "[";
43     protected static final String JavaDoc BRAKE_CLOSE = "] ";
44
45     private String JavaDoc name ;
46
47     private int level = 0;
48
49     public PoorMansLoggerImpl( String JavaDoc name)
50     {
51         this.name = name ;
52     }
53
54     protected int getLevel()
55     {
56         return level;
57     }
58
59     void setLevel(int pLevel)
60     {
61         level = pLevel;
62     }
63
64     public String JavaDoc getName()
65     {
66         return name;
67     }
68
69     /**
70      * generate a message for loglevel DEBUG
71      * @param pObject the message Object
72      */

73     public void debug(Object JavaDoc pObject)
74     {
75         debug(pObject, null);
76     }
77
78     public void debug(Object JavaDoc message, Throwable JavaDoc t)
79     {
80         if (DEBUG >= getLevel())
81         {
82             log(STR_DEBUG_MSG, message, t);
83         }
84     }
85
86     public void safeDebug(String JavaDoc message,Object JavaDoc obj)
87     {
88         safeDebug(message,obj,null);
89     }
90
91     public void safeDebug(String JavaDoc message,Object JavaDoc obj,Throwable JavaDoc t)
92     {
93         if(DEBUG >= getLevel())
94         {
95             String JavaDoc toString = null;
96             if(obj != null)
97             {
98                 try
99                 {
100                     toString = obj.toString();
101                 }
102                 catch(Throwable JavaDoc throwable)
103                 {
104                     toString = "BAD toString() impl for "+obj.getClass().getName();
105                 }
106             }
107             log(STR_DEBUG_MSG,message + " : " + toString,t);
108         }
109     }
110
111
112     /**
113      * generate a message for loglevel INFO
114      * @param pObject the message Object
115      */

116     public void info(Object JavaDoc pObject)
117     {
118         info(pObject, null);
119     }
120
121     public void info(Object JavaDoc message, Throwable JavaDoc t)
122     {
123         if (INFO >= getLevel())
124         {
125             log(STR_INFO_MSG, message, t);
126         }
127     }
128
129     public void safeInfo(String JavaDoc message,Object JavaDoc obj)
130     {
131         safeInfo(message,obj,null);
132     }
133
134     public void safeInfo(String JavaDoc message,Object JavaDoc obj,Throwable JavaDoc t)
135     {
136         if(INFO >= getLevel())
137         {
138             String JavaDoc toString = null;
139             if(obj != null)
140             {
141                 try
142                 {
143                     toString = obj.toString();
144                 }
145                 catch(Throwable JavaDoc throwable)
146                 {
147                     toString = "BAD toString() impl for "+obj.getClass().getName();
148                 }
149             }
150             log(STR_INFO_MSG, message + " : " + toString,t);
151         }
152     }
153
154     /**
155      * generate a message for loglevel WARN
156      * @param pObject the message Object
157      */

158     public void warn(Object JavaDoc pObject)
159     {
160         warn(pObject, null);
161     }
162
163     public void warn(Object JavaDoc message, Throwable JavaDoc t)
164     {
165         if (WARN >= getLevel())
166         {
167             log(STR_WARN_MSG, message, t);
168         }
169     }
170
171     public void safeWarn(String JavaDoc message,Object JavaDoc obj)
172     {
173         safeWarn(message,obj,null);
174     }
175
176     public void safeWarn(String JavaDoc message,Object JavaDoc obj,Throwable JavaDoc t)
177     {
178         if(WARN >= getLevel())
179         {
180             String JavaDoc toString = null;
181             if(obj != null)
182             {
183                 try
184                 {
185                     toString = obj.toString();
186                 }
187                 catch(Throwable JavaDoc throwable)
188                 {
189                     toString = "BAD toString() impl for "+obj.getClass().getName();
190                 }
191             }
192             log(STR_WARN_MSG,message + " : " + toString,t);
193         }
194     }
195
196     /**
197      * generate a message for loglevel ERROR
198      * @param pObject the message Object
199      */

200     public void error(Object JavaDoc pObject)
201     {
202         error(pObject, null);
203     }
204
205     public void error(Object JavaDoc message, Throwable JavaDoc t)
206     {
207         if (ERROR >= getLevel())
208         {
209             log(STR_ERROR_MSG, message, t);
210         }
211     }
212
213     public void safeError(String JavaDoc message,Object JavaDoc obj)
214     {
215         safeError(message,obj,null);
216     }
217
218     public void safeError(String JavaDoc message,Object JavaDoc obj,Throwable JavaDoc t)
219     {
220         if(ERROR >= getLevel())
221         {
222             String JavaDoc toString = null;
223             if(obj != null)
224             {
225                 try
226                 {
227                     toString = obj.toString();
228                 }
229                 catch(Throwable JavaDoc throwable)
230                 {
231                     toString = "BAD toString() impl for "+obj.getClass().getName();
232                 }
233             }
234             log(STR_ERROR_MSG,message + " : " + toString,t);
235         }
236     }
237
238     /**
239      * generate a message for loglevel FATAL
240      * @param pObject the message Object
241      */

242     public void fatal(Object JavaDoc pObject)
243     {
244         fatal(pObject, null);
245     }
246
247     public void fatal(Object JavaDoc message, Throwable JavaDoc t)
248     {
249         if (FATAL >= getLevel())
250         {
251             log(STR_FATAL_MSG, message, t);
252         }
253     }
254
255     public void safeFatal(String JavaDoc message,Object JavaDoc obj)
256     {
257         safeFatal(message,obj,null);
258     }
259
260     public void safeFatal(String JavaDoc message,Object JavaDoc obj,Throwable JavaDoc t)
261     {
262         if(FATAL >= getLevel())
263         {
264             String JavaDoc toString = null;
265             if(obj != null)
266             {
267                 try
268                 {
269                     toString = obj.toString();
270                 }
271                 catch(Throwable JavaDoc throwable)
272                 {
273                     toString = "BAD toString() impl for "+obj.getClass().getName();
274                 }
275             }
276             log(STR_FATAL_MSG,message + " : " + toString,t);
277         }
278     }
279
280
281     public boolean isDebugEnabled()
282     {
283         return isEnabledFor(DEBUG);
284     }
285
286     public boolean isEnabledFor(int priority)
287     {
288         return priority >= getLevel();
289     }
290
291     protected void log(String JavaDoc aLevel, Object JavaDoc obj, Throwable JavaDoc t)
292     {
293         System.out.print(BRAKE_OPEN + name + BRAKE_CLOSE + aLevel);
294         if (obj != null && obj instanceof Throwable JavaDoc)
295         {
296             try
297             {
298                 System.out.println(((Throwable JavaDoc) obj).getMessage());
299                 ((Throwable JavaDoc) obj).printStackTrace();
300             }
301             catch (Throwable JavaDoc ignored)
302             {
303                 /*logging should be failsafe*/
304             }
305         }
306         else
307         {
308             System.out.println(obj);
309         }
310
311         if (t != null)
312         {
313             try
314             {
315                 System.out.println(t.getMessage());
316                 t.printStackTrace();
317             }
318             catch (Throwable JavaDoc ignored)
319             {
320                 /*logging should be failsafe*/
321             }
322         }
323     }
324
325     /*
326      * @see org.apache.ojb.broker.util.configuration.Configurable#configure(Configuration)
327      */

328     public void configure(Configuration config) throws ConfigurationException
329     {
330         LoggingConfiguration lc = (LoggingConfiguration) config;
331         String JavaDoc levelName = lc.getLogLevel(name);
332         setLevel(levelName);
333     }
334
335     public void setLevel(String JavaDoc levelName)
336     {
337         if (levelName.equalsIgnoreCase(STR_DEBUG))
338         {
339             level = DEBUG;
340         }
341         else if (levelName.equalsIgnoreCase(STR_INFO))
342         {
343             level = INFO;
344         }
345         else if (levelName.equalsIgnoreCase(STR_WARN))
346         {
347             level = WARN;
348         }
349         else if (levelName.equalsIgnoreCase(STR_ERROR))
350         {
351             level = ERROR;
352         }
353         else if (levelName.equalsIgnoreCase(STR_FATAL))
354         {
355             level = FATAL;
356         }
357         else
358         {
359             level = WARN;
360         }
361     }
362 }
363
Popular Tags