View Javadoc

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