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  
21  import java.io.Serializable;
22  import java.util.logging.Level;
23  import java.util.logging.Logger;
24  import java.util.logging.LogRecord;
25  import java.util.StringTokenizer;
26  import java.io.PrintWriter;
27  import java.io.StringWriter;
28  
29  import org.apache.commons.logging.Log;
30  
31  
32  /***
33   * <p>Implementation of the <code>org.apache.commons.logging.Log</code>
34   * interface that wraps the standard JDK logging mechanisms that are
35   * available in SourceForge's Lumberjack for JDKs prior to 1.4.</p>
36   *
37   * @author <a href="mailto:sanders@apache.org">Scott Sanders</a>
38   * @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
39   * @author <a href="mailto:donaldp@apache.org">Peter Donald</a>
40   * @author <a href="mailto:vince256@comcast.net">Vince Eagen</a>
41   * @version $Revision: 371403 $ $Date: 2006-01-22 22:07:31 +0000 (Sun, 22 Jan 2006) $
42   * @since 1.1
43   */
44  
45  public class Jdk13LumberjackLogger implements Log, Serializable {
46  
47  
48      // ----------------------------------------------------- Instance Variables
49  
50  
51      /***
52       * The underlying Logger implementation we are using.
53       */
54      protected transient Logger logger = null;
55      protected String name = null;
56      private String sourceClassName = "unknown";
57      private String sourceMethodName = "unknown";
58      private boolean classAndMethodFound = false;
59  
60  
61      /***
62       * This member variable simply ensures that any attempt to initialise
63       * this class in a pre-1.4 JVM will result in an ExceptionInInitializerError.
64       * It must not be private, as an optimising compiler could detect that it
65       * is not used and optimise it away.
66       */
67      protected static final Level dummyLevel = Level.FINE;
68  
69      // ----------------------------------------------------------- Constructors
70  
71  
72      /***
73       * Construct a named instance of this Logger.
74       *
75       * @param name Name of the logger to be constructed
76       */
77      public Jdk13LumberjackLogger(String name) {
78  
79          this.name = name;
80          logger = getLogger();
81  
82      }
83  
84  
85      // --------------------------------------------------------- Public Methods
86  
87  
88      private void log( Level level, String msg, Throwable ex ) {
89          if( getLogger().isLoggable(level) ) {
90              LogRecord record = new LogRecord(level, msg);
91              if( !classAndMethodFound ) {
92                  getClassAndMethod();
93              }
94              record.setSourceClassName(sourceClassName);
95              record.setSourceMethodName(sourceMethodName);
96              if( ex != null ) {
97                  record.setThrown(ex);
98              }
99              getLogger().log(record);
100         }
101     }
102 
103     /***
104      * <p>Gets the class and method by looking at the stack trace for the
105      * first entry that is not this class.</p>
106      */
107     private void getClassAndMethod() {
108         try {
109             Throwable throwable = new Throwable();
110             throwable.fillInStackTrace();
111             StringWriter stringWriter = new StringWriter();
112             PrintWriter printWriter = new PrintWriter( stringWriter );
113             throwable.printStackTrace( printWriter );
114             String traceString = stringWriter.getBuffer().toString();
115             StringTokenizer tokenizer =
116                 new StringTokenizer( traceString, "\n" );
117             tokenizer.nextToken();
118             String line = tokenizer.nextToken();
119             while ( line.indexOf( this.getClass().getName() )  == -1 ) {
120                 line = tokenizer.nextToken();
121             }
122             while ( line.indexOf( this.getClass().getName() ) >= 0 ) {
123                 line = tokenizer.nextToken();
124             }
125             int start = line.indexOf( "at " ) + 3;
126             int end = line.indexOf( '(' );
127             String temp = line.substring( start, end );
128             int lastPeriod = temp.lastIndexOf( '.' );
129             sourceClassName = temp.substring( 0, lastPeriod );
130             sourceMethodName = temp.substring( lastPeriod + 1 );
131         } catch ( Exception ex ) {
132             // ignore - leave class and methodname unknown
133         }
134         classAndMethodFound = true;
135     }
136 
137     /***
138      * Logs a message with <code>java.util.logging.Level.FINE</code>.
139      *
140      * @param message to log
141      * @see org.apache.commons.logging.Log#debug(Object)
142      */
143     public void debug(Object message) {
144         log(Level.FINE, String.valueOf(message), null);
145     }
146 
147 
148     /***
149      * Logs a message with <code>java.util.logging.Level.FINE</code>.
150      *
151      * @param message to log
152      * @param exception log this cause
153      * @see org.apache.commons.logging.Log#debug(Object, Throwable)
154      */
155     public void debug(Object message, Throwable exception) {
156         log(Level.FINE, String.valueOf(message), exception);
157     }
158 
159 
160     /***
161      * Logs a message with <code>java.util.logging.Level.SEVERE</code>.
162      *
163      * @param message to log
164      * @see org.apache.commons.logging.Log#error(Object)
165      */
166     public void error(Object message) {
167         log(Level.SEVERE, String.valueOf(message), null);
168     }
169 
170 
171     /***
172      * Logs a message with <code>java.util.logging.Level.SEVERE</code>.
173      *
174      * @param message to log
175      * @param exception log this cause
176      * @see org.apache.commons.logging.Log#error(Object, Throwable)
177      */
178     public void error(Object message, Throwable exception) {
179         log(Level.SEVERE, String.valueOf(message), exception);
180     }
181 
182 
183     /***
184      * Logs a message with <code>java.util.logging.Level.SEVERE</code>.
185      *
186      * @param message to log
187      * @see org.apache.commons.logging.Log#fatal(Object)
188      */
189     public void fatal(Object message) {
190         log(Level.SEVERE, String.valueOf(message), null);
191     }
192 
193 
194     /***
195      * Logs a message with <code>java.util.logging.Level.SEVERE</code>.
196      *
197      * @param message to log
198      * @param exception log this cause
199      * @see org.apache.commons.logging.Log#fatal(Object, Throwable)
200      */
201     public void fatal(Object message, Throwable exception) {
202         log(Level.SEVERE, String.valueOf(message), exception);
203     }
204 
205 
206     /***
207      * Return the native Logger instance we are using.
208      */
209     public Logger getLogger() {
210         if (logger == null) {
211             logger = Logger.getLogger(name);
212         }
213         return (logger);
214     }
215 
216 
217     /***
218      * Logs a message with <code>java.util.logging.Level.INFO</code>.
219      *
220      * @param message to log
221      * @see org.apache.commons.logging.Log#info(Object)
222      */
223     public void info(Object message) {
224         log(Level.INFO, String.valueOf(message), null);
225     }
226 
227 
228     /***
229      * Logs a message with <code>java.util.logging.Level.INFO</code>.
230      *
231      * @param message to log
232      * @param exception log this cause
233      * @see org.apache.commons.logging.Log#info(Object, Throwable)
234      */
235     public void info(Object message, Throwable exception) {
236         log(Level.INFO, String.valueOf(message), exception);
237     }
238 
239 
240     /***
241      * Is debug logging currently enabled?
242      */
243     public boolean isDebugEnabled() {
244         return (getLogger().isLoggable(Level.FINE));
245     }
246 
247 
248     /***
249      * Is error logging currently enabled?
250      */
251     public boolean isErrorEnabled() {
252         return (getLogger().isLoggable(Level.SEVERE));
253     }
254 
255 
256     /***
257      * Is fatal logging currently enabled?
258      */
259     public boolean isFatalEnabled() {
260         return (getLogger().isLoggable(Level.SEVERE));
261     }
262 
263 
264     /***
265      * Is info logging currently enabled?
266      */
267     public boolean isInfoEnabled() {
268         return (getLogger().isLoggable(Level.INFO));
269     }
270 
271 
272     /***
273      * Is trace logging currently enabled?
274      */
275     public boolean isTraceEnabled() {
276         return (getLogger().isLoggable(Level.FINEST));
277     }
278 
279 
280     /***
281      * Is warn logging currently enabled?
282      */
283     public boolean isWarnEnabled() {
284         return (getLogger().isLoggable(Level.WARNING));
285     }
286 
287 
288     /***
289      * Logs a message with <code>java.util.logging.Level.FINEST</code>.
290      *
291      * @param message to log
292      * @see org.apache.commons.logging.Log#trace(Object)
293      */
294     public void trace(Object message) {
295         log(Level.FINEST, String.valueOf(message), null);
296     }
297 
298 
299     /***
300      * Logs a message with <code>java.util.logging.Level.FINEST</code>.
301      *
302      * @param message to log
303      * @param exception log this cause
304      * @see org.apache.commons.logging.Log#trace(Object, Throwable)
305      */
306     public void trace(Object message, Throwable exception) {
307         log(Level.FINEST, String.valueOf(message), exception);
308     }
309 
310 
311     /***
312      * Logs a message with <code>java.util.logging.Level.WARNING</code>.
313      *
314      * @param message to log
315      * @see org.apache.commons.logging.Log#warn(Object)
316      */
317     public void warn(Object message) {
318         log(Level.WARNING, String.valueOf(message), null);
319     }
320 
321 
322     /***
323      * Logs a message with <code>java.util.logging.Level.WARNING</code>.
324      *
325      * @param message to log
326      * @param exception log this cause
327      * @see org.apache.commons.logging.Log#warn(Object, Throwable)
328      */
329     public void warn(Object message, Throwable exception) {
330         log(Level.WARNING, String.valueOf(message), exception);
331     }
332 
333 
334 }