1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.sf.asterisk.util.impl;
18
19 import java.io.Serializable;
20 import net.sf.asterisk.util.Log;
21 import org.apache.log4j.Logger;
22 import org.apache.log4j.Priority;
23 import org.apache.log4j.Level;
24
25 /***
26 * <p>
27 * Implementation of {@link Log} that maps directly to a Log4J <strong>Logger</strong>.
28 * Initial configuration of the corresponding Logger instances should be done in
29 * the usual manner, as outlined in the Log4J documentation.
30 * </p>
31 *
32 * @author <a href="mailto:sanders@apache.org">Scott Sanders</a>
33 * @author Rod Waldhoff
34 * @author Robert Burrell Donkin
35 * @version $Id: Log4JLogger.java,v 1.1 2005/04/20 18:22:13 srt Exp $
36 */
37 public class Log4JLogger implements Log, Serializable
38 {
39
40
41
42 /***
43 * The serial version identifier.
44 */
45 private static final long serialVersionUID = 3545240215095883829L;
46
47 /*** The fully qualified name of the Log4JLogger class. */
48 private static final String FQCN = Log4JLogger.class.getName();
49
50 private static final boolean is12 = Priority.class
51 .isAssignableFrom(Level.class);
52
53 /*** Log to this logger */
54 private transient Logger logger = null;
55
56 /*** Logger name */
57 private String name = null;
58
59
60
61 public Log4JLogger()
62 {
63 }
64
65 /***
66 * Base constructor.
67 */
68 public Log4JLogger(Class clazz)
69 {
70 this.name = clazz.getName();
71 this.logger = getLogger();
72 }
73
74
75
76 /***
77 * Log a message to the Log4j Logger with <code>TRACE</code> priority.
78 * Currently logs to <code>DEBUG</code> level in Log4J.
79 */
80 public void trace(Object message)
81 {
82 if (is12)
83 {
84 getLogger().log(FQCN, (Priority) Level.DEBUG, message, null);
85 }
86 else
87 {
88 getLogger().log(FQCN, Level.DEBUG, message, null);
89 }
90 }
91
92 /***
93 * Log an error to the Log4j Logger with <code>TRACE</code> priority.
94 * Currently logs to <code>DEBUG</code> level in Log4J.
95 */
96 public void trace(Object message, Throwable t)
97 {
98 if (is12)
99 {
100 getLogger().log(FQCN, (Priority) Level.DEBUG, message, t);
101 }
102 else
103 {
104 getLogger().log(FQCN, Level.DEBUG, message, t);
105 }
106 }
107
108 /***
109 * Log a message to the Log4j Logger with <code>DEBUG</code> priority.
110 */
111 public void debug(Object message)
112 {
113 if (is12)
114 {
115 getLogger().log(FQCN, (Priority) Level.DEBUG, message, null);
116 }
117 else
118 {
119 getLogger().log(FQCN, Level.DEBUG, message, null);
120 }
121 }
122
123 /***
124 * Log an error to the Log4j Logger with <code>DEBUG</code> priority.
125 */
126 public void debug(Object message, Throwable t)
127 {
128 if (is12)
129 {
130 getLogger().log(FQCN, (Priority) Level.DEBUG, message, t);
131 }
132 else
133 {
134 getLogger().log(FQCN, Level.DEBUG, message, t);
135 }
136 }
137
138 /***
139 * Log a message to the Log4j Logger with <code>INFO</code> priority.
140 */
141 public void info(Object message)
142 {
143 if (is12)
144 {
145 getLogger().log(FQCN, (Priority) Level.INFO, message, null);
146 }
147 else
148 {
149 getLogger().log(FQCN, Level.INFO, message, null);
150 }
151 }
152
153 /***
154 * Log an error to the Log4j Logger with <code>INFO</code> priority.
155 */
156 public void info(Object message, Throwable t)
157 {
158 if (is12)
159 {
160 getLogger().log(FQCN, (Priority) Level.INFO, message, t);
161 }
162 else
163 {
164 getLogger().log(FQCN, Level.INFO, message, t);
165 }
166 }
167
168 /***
169 * Log a message to the Log4j Logger with <code>WARN</code> priority.
170 */
171 public void warn(Object message)
172 {
173 if (is12)
174 {
175 getLogger().log(FQCN, (Priority) Level.WARN, message, null);
176 }
177 else
178 {
179 getLogger().log(FQCN, Level.WARN, message, null);
180 }
181 }
182
183 /***
184 * Log an error to the Log4j Logger with <code>WARN</code> priority.
185 */
186 public void warn(Object message, Throwable t)
187 {
188 if (is12)
189 {
190 getLogger().log(FQCN, (Priority) Level.WARN, message, t);
191 }
192 else
193 {
194 getLogger().log(FQCN, Level.WARN, message, t);
195 }
196 }
197
198 /***
199 * Log a message to the Log4j Logger with <code>ERROR</code> priority.
200 */
201 public void error(Object message)
202 {
203 if (is12)
204 {
205 getLogger().log(FQCN, (Priority) Level.ERROR, message, null);
206 }
207 else
208 {
209 getLogger().log(FQCN, Level.ERROR, message, null);
210 }
211 }
212
213 /***
214 * Log an error to the Log4j Logger with <code>ERROR</code> priority.
215 */
216 public void error(Object message, Throwable t)
217 {
218 if (is12)
219 {
220 getLogger().log(FQCN, (Priority) Level.ERROR, message, t);
221 }
222 else
223 {
224 getLogger().log(FQCN, Level.ERROR, message, t);
225 }
226 }
227
228 /***
229 * Log a message to the Log4j Logger with <code>FATAL</code> priority.
230 */
231 public void fatal(Object message)
232 {
233 if (is12)
234 {
235 getLogger().log(FQCN, (Priority) Level.FATAL, message, null);
236 }
237 else
238 {
239 getLogger().log(FQCN, Level.FATAL, message, null);
240 }
241 }
242
243 /***
244 * Log an error to the Log4j Logger with <code>FATAL</code> priority.
245 */
246 public void fatal(Object message, Throwable t)
247 {
248 if (is12)
249 {
250 getLogger().log(FQCN, (Priority) Level.FATAL, message, t);
251 }
252 else
253 {
254 getLogger().log(FQCN, Level.FATAL, message, t);
255 }
256 }
257
258 /***
259 * Return the native Logger instance we are using.
260 */
261 public Logger getLogger()
262 {
263 if (logger == null)
264 {
265 logger = Logger.getLogger(name);
266 }
267 return (this.logger);
268 }
269
270 /***
271 * Check whether the Log4j Logger used is enabled for <code>DEBUG</code>
272 * priority.
273 */
274 public boolean isDebugEnabled()
275 {
276 return getLogger().isDebugEnabled();
277 }
278
279 /***
280 * Check whether the Log4j Logger used is enabled for <code>ERROR</code>
281 * priority.
282 */
283 public boolean isErrorEnabled()
284 {
285 if (is12)
286 {
287 return getLogger().isEnabledFor((Priority) Level.ERROR);
288 }
289 else
290 {
291 return getLogger().isEnabledFor(Level.ERROR);
292 }
293 }
294
295 /***
296 * Check whether the Log4j Logger used is enabled for <code>FATAL</code>
297 * priority.
298 */
299 public boolean isFatalEnabled()
300 {
301 if (is12)
302 {
303 return getLogger().isEnabledFor((Priority) Level.FATAL);
304 }
305 else
306 {
307 return getLogger().isEnabledFor(Level.FATAL);
308 }
309 }
310
311 /***
312 * Check whether the Log4j Logger used is enabled for <code>INFO</code>
313 * priority.
314 */
315 public boolean isInfoEnabled()
316 {
317 return getLogger().isInfoEnabled();
318 }
319
320 /***
321 * Check whether the Log4j Logger used is enabled for <code>TRACE</code>
322 * priority. For Log4J, this returns the value of
323 * <code>isDebugEnabled()</code>
324 */
325 public boolean isTraceEnabled()
326 {
327 return getLogger().isDebugEnabled();
328 }
329
330 /***
331 * Check whether the Log4j Logger used is enabled for <code>WARN</code>
332 * priority.
333 */
334 public boolean isWarnEnabled()
335 {
336 if (is12)
337 {
338 return getLogger().isEnabledFor((Priority) Level.WARN);
339 }
340 else
341 {
342 return getLogger().isEnabledFor(Level.WARN);
343 }
344 }
345 }