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  
18  package org.apache.commons.logging.impl;
19  
20  import java.io.Serializable;
21  import org.apache.commons.logging.Log;
22  import org.apache.log4j.Logger;
23  import org.apache.log4j.Priority;
24  import org.apache.log4j.Level;
25  
26  /***
27   * <p>Implementation of {@link Log} that maps directly to a Log4J
28   * <strong>Logger</strong>.  Initial configuration of the corresponding
29   * Logger instances should be done in the usual manner, as outlined in
30   * the Log4J documentation.</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.11 2004/05/19 21:01:23 rdonkin Exp $
36   */
37  public class Log4JLogger implements Log, Serializable {
38  
39  
40      // ------------------------------------------------------------- Attributes
41  
42      /*** The fully qualified name of the Log4JLogger class. */
43      private static final String FQCN = Log4JLogger.class.getName();
44      
45      private static final boolean is12 = Priority.class.isAssignableFrom(Level.class);
46  
47      /*** Log to this logger */
48      private transient Logger logger = null;
49  
50      /*** Logger name */
51      private String name = null;
52  
53  
54      // ------------------------------------------------------------ Constructor
55  
56      public Log4JLogger() {
57      }
58  
59  
60      /***
61       * Base constructor.
62       */
63      public Log4JLogger(String name) {
64          this.name = name;
65          this.logger = getLogger();
66      }
67  
68      /*** For use with a log4j factory.
69       */
70      public Log4JLogger(Logger logger ) {
71          this.name = logger.getName();
72          this.logger=logger;
73      }
74  
75  
76      // --------------------------------------------------------- Implementation
77  
78  
79      /***
80       * Log a message to the Log4j Logger with <code>TRACE</code> priority.
81       * Currently logs to <code>DEBUG</code> level in Log4J.
82       */
83      public void trace(Object message) {
84          if(is12) {
85              getLogger().log(FQCN, (Priority) Level.DEBUG, message, null );
86          } else {
87              getLogger().log(FQCN, Level.DEBUG, message, null );
88          }
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          if(is12) {
98              getLogger().log(FQCN, (Priority) Level.DEBUG, message, t );
99          } else {
100             getLogger().log(FQCN, Level.DEBUG, message, t );
101         }
102     }
103 
104 
105     /***
106      * Log a message to the Log4j Logger with <code>DEBUG</code> priority.
107      */
108     public void debug(Object message) {
109         if(is12) {
110             getLogger().log(FQCN, (Priority) Level.DEBUG, message, null );
111         } else {
112             getLogger().log(FQCN, Level.DEBUG, message, null );
113         }
114     }
115 
116     /***
117      * Log an error to the Log4j Logger with <code>DEBUG</code> priority.
118      */
119     public void debug(Object message, Throwable t) {
120         if(is12) {
121             getLogger().log(FQCN, (Priority) Level.DEBUG, message, t );
122         } else {
123             getLogger().log(FQCN, Level.DEBUG, message, t );
124         }
125     }
126 
127 
128     /***
129      * Log a message to the Log4j Logger with <code>INFO</code> priority.
130      */
131     public void info(Object message) {
132         if(is12) {
133             getLogger().log(FQCN, (Priority) Level.INFO, message, null );
134         } else {
135             getLogger().log(FQCN, Level.INFO, message, null );
136         }    
137     }
138 
139 
140     /***
141      * Log an error to the Log4j Logger with <code>INFO</code> priority.
142      */
143     public void info(Object message, Throwable t) {
144         if(is12) {
145             getLogger().log(FQCN, (Priority) Level.INFO, message, t );
146         } else {
147             getLogger().log(FQCN, Level.INFO, message, t );
148         }
149     }
150 
151 
152     /***
153      * Log a message to the Log4j Logger with <code>WARN</code> priority.
154      */
155     public void warn(Object message) {
156         if(is12) {
157             getLogger().log(FQCN, (Priority) Level.WARN, message, null );
158         } else {
159             getLogger().log(FQCN, Level.WARN, message, null );
160         }
161     }
162 
163 
164     /***
165      * Log an error to the Log4j Logger with <code>WARN</code> priority.
166      */
167     public void warn(Object message, Throwable t) {
168         if(is12) {
169             getLogger().log(FQCN, (Priority) Level.WARN, message, t );
170         } else {
171             getLogger().log(FQCN, Level.WARN, message, t );
172         }
173     }
174 
175 
176     /***
177      * Log a message to the Log4j Logger with <code>ERROR</code> priority.
178      */
179     public void error(Object message) {
180         if(is12) {
181             getLogger().log(FQCN, (Priority) Level.ERROR, message, null );
182         } else {
183             getLogger().log(FQCN, Level.ERROR, message, null );
184         }
185     }
186 
187 
188     /***
189      * Log an error to the Log4j Logger with <code>ERROR</code> priority.
190      */
191     public void error(Object message, Throwable t) {
192         if(is12) {
193             getLogger().log(FQCN, (Priority) Level.ERROR, message, t );
194         } else {
195             getLogger().log(FQCN, Level.ERROR, message, t );
196         }
197     }
198 
199 
200     /***
201      * Log a message to the Log4j Logger with <code>FATAL</code> priority.
202      */
203     public void fatal(Object message) {
204         if(is12) {
205             getLogger().log(FQCN, (Priority) Level.FATAL, message, null );
206         } else {
207             getLogger().log(FQCN, Level.FATAL, message, null );
208         }
209     }
210 
211 
212     /***
213      * Log an error to the Log4j Logger with <code>FATAL</code> priority.
214      */
215     public void fatal(Object message, Throwable t) {
216         if(is12) {
217             getLogger().log(FQCN, (Priority) Level.FATAL, message, t );
218         } else {
219             getLogger().log(FQCN, Level.FATAL, message, t );
220         }
221     }
222 
223 
224     /***
225      * Return the native Logger instance we are using.
226      */
227     public Logger getLogger() {
228         if (logger == null) {
229             logger = Logger.getLogger(name);
230         }
231         return (this.logger);
232     }
233 
234 
235     /***
236      * Check whether the Log4j Logger used is enabled for <code>DEBUG</code> priority.
237      */
238     public boolean isDebugEnabled() {
239         return getLogger().isDebugEnabled();
240     }
241 
242 
243      /***
244      * Check whether the Log4j Logger used is enabled for <code>ERROR</code> priority.
245      */
246     public boolean isErrorEnabled() {
247         if(is12) {
248             return getLogger().isEnabledFor((Priority) Level.ERROR);
249         } else {
250             return getLogger().isEnabledFor(Level.ERROR);
251         }
252     }
253 
254 
255     /***
256      * Check whether the Log4j Logger used is enabled for <code>FATAL</code> priority.
257      */
258     public boolean isFatalEnabled() {
259         if(is12) {
260             return getLogger().isEnabledFor((Priority) Level.FATAL);
261         } else {
262             return getLogger().isEnabledFor(Level.FATAL);
263         }
264     }
265 
266 
267     /***
268      * Check whether the Log4j Logger used is enabled for <code>INFO</code> priority.
269      */
270     public boolean isInfoEnabled() {
271         return getLogger().isInfoEnabled();
272     }
273 
274 
275     /***
276      * Check whether the Log4j Logger used is enabled for <code>TRACE</code> priority.
277      * For Log4J, this returns the value of <code>isDebugEnabled()</code>
278      */
279     public boolean isTraceEnabled() {
280         return getLogger().isDebugEnabled();
281     }
282 
283     /***
284      * Check whether the Log4j Logger used is enabled for <code>WARN</code> priority.
285      */
286     public boolean isWarnEnabled() {
287         if(is12) {
288             return getLogger().isEnabledFor((Priority) Level.WARN);
289         } else {
290             return getLogger().isEnabledFor(Level.WARN);
291         }
292     }
293 }