1 | |
package org.apache.ojb.broker.util.logging; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
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 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
public class CommonsLoggerImpl implements Logger |
33 | |
{ |
34 | |
private String name; |
35 | |
private transient Log log; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
public CommonsLoggerImpl(String aName) |
42 | |
{ |
43 | |
this.name = aName; |
44 | |
} |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
public Log getLog() |
51 | |
{ |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
if(log == null) |
57 | |
{ |
58 | |
log = LogFactory.getLog(name); |
59 | |
} |
60 | |
return log; |
61 | |
} |
62 | |
|
63 | |
|
64 | |
|
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 | |
|
82 | |
|
83 | |
public void debug(Object pObject) |
84 | |
{ |
85 | |
getLog().debug(pObject); |
86 | |
} |
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
public void info(Object pObject) |
92 | |
{ |
93 | |
getLog().info(pObject); |
94 | |
} |
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
public void warn(Object pObject) |
100 | |
{ |
101 | |
getLog().warn(pObject); |
102 | |
} |
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
public void error(Object pObject) |
108 | |
{ |
109 | |
getLog().error(pObject); |
110 | |
} |
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
public void fatal(Object pObject) |
116 | |
{ |
117 | |
getLog().fatal(pObject); |
118 | |
} |
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
public void debug(Object message, Throwable obj) |
124 | |
{ |
125 | |
getLog().debug(message, obj); |
126 | |
} |
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
public void info(Object message, Throwable obj) |
132 | |
{ |
133 | |
getLog().info(message, obj); |
134 | |
} |
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
public void warn(Object message, Throwable obj) |
140 | |
{ |
141 | |
getLog().warn(message, obj); |
142 | |
} |
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
public void error(Object message, Throwable obj) |
148 | |
{ |
149 | |
getLog().error(message, obj); |
150 | |
} |
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
public void fatal(Object message, Throwable obj) |
156 | |
{ |
157 | |
getLog().fatal(message, obj); |
158 | |
} |
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
public boolean isDebugEnabled() |
164 | |
{ |
165 | |
return getLog().isDebugEnabled(); |
166 | |
} |
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
public String getName() |
172 | |
{ |
173 | |
return name; |
174 | |
} |
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
public void safeDebug(String message, Object obj) |
180 | |
{ |
181 | |
if (getLog().isDebugEnabled()) |
182 | |
{ |
183 | |
String toString = safeToString(obj); |
184 | |
getLog().debug(message + " : " + toString); |
185 | |
} |
186 | |
} |
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
public void safeDebug(String message, Object obj, Throwable t) |
192 | |
{ |
193 | |
if (getLog().isDebugEnabled()) |
194 | |
{ |
195 | |
String toString = safeToString(obj); |
196 | |
getLog().debug(message + " : " + toString, t); |
197 | |
} |
198 | |
} |
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
public void safeInfo(String message, Object obj) |
204 | |
{ |
205 | |
if (getLog().isInfoEnabled()) |
206 | |
{ |
207 | |
String toString = safeToString(obj); |
208 | |
getLog().info(message + " : " + toString); |
209 | |
} |
210 | |
} |
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
public void safeInfo(String message, Object obj, Throwable t) |
216 | |
{ |
217 | |
if (getLog().isInfoEnabled()) |
218 | |
{ |
219 | |
String toString = safeToString(obj); |
220 | |
getLog().info(message + " : " + toString, t); |
221 | |
} |
222 | |
} |
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
public void safeWarn(String message, Object obj) |
228 | |
{ |
229 | |
if (getLog().isWarnEnabled()) |
230 | |
{ |
231 | |
String toString = safeToString(obj); |
232 | |
getLog().warn(message + " : " + toString); |
233 | |
} |
234 | |
} |
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | |
public void safeWarn(String message, Object obj, Throwable t) |
240 | |
{ |
241 | |
if (getLog().isWarnEnabled()) |
242 | |
{ |
243 | |
String toString = safeToString(obj); |
244 | |
getLog().warn(message + " : " + toString, t); |
245 | |
} |
246 | |
} |
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | |
public void safeError(String message, Object obj) |
252 | |
{ |
253 | |
if (getLog().isErrorEnabled()) |
254 | |
{ |
255 | |
String toString = safeToString(obj); |
256 | |
getLog().error(message + " : " + toString); |
257 | |
} |
258 | |
} |
259 | |
|
260 | |
|
261 | |
|
262 | |
|
263 | |
public void safeError(String message, Object obj, Throwable t) |
264 | |
{ |
265 | |
if (getLog().isErrorEnabled()) |
266 | |
{ |
267 | |
String toString = safeToString(obj); |
268 | |
getLog().error(message + " : " + toString, t); |
269 | |
} |
270 | |
} |
271 | |
|
272 | |
|
273 | |
|
274 | |
|
275 | |
public void safeFatal(String message, Object obj) |
276 | |
{ |
277 | |
if (getLog().isFatalEnabled()) |
278 | |
{ |
279 | |
String toString = safeToString(obj); |
280 | |
getLog().fatal(message + " : " + toString); |
281 | |
} |
282 | |
} |
283 | |
|
284 | |
|
285 | |
|
286 | |
|
287 | |
public void safeFatal(String message, Object obj, Throwable t) |
288 | |
{ |
289 | |
if (getLog().isFatalEnabled()) |
290 | |
{ |
291 | |
String toString = safeToString(obj); |
292 | |
getLog().fatal(message + " : " + toString, t); |
293 | |
} |
294 | |
} |
295 | |
|
296 | |
|
297 | |
|
298 | |
|
299 | |
private String safeToString(Object obj) |
300 | |
{ |
301 | |
String toString = null; |
302 | |
if (obj != null) |
303 | |
{ |
304 | |
try |
305 | |
{ |
306 | |
toString = obj.toString(); |
307 | |
} |
308 | |
catch (Throwable ex) |
309 | |
{ |
310 | |
toString = "BAD toString() impl for " + obj.getClass().getName(); |
311 | |
} |
312 | |
} |
313 | |
return toString; |
314 | |
} |
315 | |
|
316 | |
|
317 | |
|
318 | |
|
319 | |
public void configure(Configuration config) throws ConfigurationException |
320 | |
{ |
321 | |
|
322 | |
} |
323 | |
} |