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: 1.6 $ $Date: 2004/06/06 21:13:43 $
42   */
43  
44  public class Jdk13LumberjackLogger implements Log, Serializable {
45  
46  
47      // ----------------------------------------------------- Instance Variables
48  
49  
50      /***
51       * The underlying Logger implementation we are using.
52       */
53      protected transient Logger logger = null;
54      protected String name = null;
55      private String sourceClassName = "unknown";
56      private String sourceMethodName = "unknown";
57      private boolean classAndMethodFound = false;
58  
59  
60      // ----------------------------------------------------------- Constructors
61  
62  
63      /***
64       * Construct a named instance of this Logger.
65       *
66       * @param name Name of the logger to be constructed
67       */
68      public Jdk13LumberjackLogger(String name) {
69  
70          this.name = name;
71          logger = getLogger();
72  
73      }
74  
75  
76      // --------------------------------------------------------- Public Methods
77  
78  
79      private void log( Level level, String msg, Throwable ex ) {
80          if( getLogger().isLoggable(level) ) {
81              LogRecord record = new LogRecord(level, msg);
82              if( !classAndMethodFound ) {
83                  getClassAndMethod();
84              }
85              record.setSourceClassName(sourceClassName);
86              record.setSourceMethodName(sourceMethodName);
87              if( ex != null ) {
88                  record.setThrown(ex);
89              }
90              getLogger().log(record);
91          }
92      }
93  
94      /***
95       * <p>Gets the class and method by looking at the stack trace for the
96       * first entry that is not this class.</p>
97       */
98      private void getClassAndMethod() {
99          try {
100             Throwable throwable = new Throwable();
101             throwable.fillInStackTrace();
102             StringWriter stringWriter = new StringWriter();
103             PrintWriter printWriter = new PrintWriter( stringWriter );
104             throwable.printStackTrace( printWriter );
105             String traceString = stringWriter.getBuffer().toString();
106             StringTokenizer tokenizer =
107                 new StringTokenizer( traceString, "\n" );
108             tokenizer.nextToken();
109             String line = tokenizer.nextToken();
110             while ( line.indexOf( this.getClass().getName() )  == -1 ) {
111                 line = tokenizer.nextToken();
112             }
113             while ( line.indexOf( this.getClass().getName() ) >= 0 ) {
114                 line = tokenizer.nextToken();
115             }
116             int start = line.indexOf( "at " ) + 3;
117             int end = line.indexOf( '(' );
118             String temp = line.substring( start, end );
119             int lastPeriod = temp.lastIndexOf( '.' );
120             sourceClassName = temp.substring( 0, lastPeriod );
121             sourceMethodName = temp.substring( lastPeriod + 1 );
122         } catch ( Exception ex ) {
123             // ignore - leave class and methodname unknown
124         }
125         classAndMethodFound = true;
126     }
127 
128     /***
129      * Log a message with debug log level.
130      */
131     public void debug(Object message) {
132         log(Level.FINE, String.valueOf(message), null);
133     }
134 
135 
136     /***
137      * Log a message and exception with debug log level.
138      */
139     public void debug(Object message, Throwable exception) {
140         log(Level.FINE, String.valueOf(message), exception);
141     }
142 
143 
144     /***
145      * Log a message with error log level.
146      */
147     public void error(Object message) {
148         log(Level.SEVERE, String.valueOf(message), null);
149     }
150 
151 
152     /***
153      * Log a message and exception with error log level.
154      */
155     public void error(Object message, Throwable exception) {
156         log(Level.SEVERE, String.valueOf(message), exception);
157     }
158 
159 
160     /***
161      * Log a message with fatal log level.
162      */
163     public void fatal(Object message) {
164         log(Level.SEVERE, String.valueOf(message), null);
165     }
166 
167 
168     /***
169      * Log a message and exception with fatal log level.
170      */
171     public void fatal(Object message, Throwable exception) {
172         log(Level.SEVERE, String.valueOf(message), exception);
173     }
174 
175 
176     /***
177      * Return the native Logger instance we are using.
178      */
179     public Logger getLogger() {
180         if (logger == null) {
181             logger = Logger.getLogger(name);
182         }
183         return (logger);
184     }
185 
186 
187     /***
188      * Log a message with info log level.
189      */
190     public void info(Object message) {
191         log(Level.INFO, String.valueOf(message), null);
192     }
193 
194 
195     /***
196      * Log a message and exception with info log level.
197      */
198     public void info(Object message, Throwable exception) {
199         log(Level.INFO, String.valueOf(message), exception);
200     }
201 
202 
203     /***
204      * Is debug logging currently enabled?
205      */
206     public boolean isDebugEnabled() {
207         return (getLogger().isLoggable(Level.FINE));
208     }
209 
210 
211     /***
212      * Is error logging currently enabled?
213      */
214     public boolean isErrorEnabled() {
215         return (getLogger().isLoggable(Level.SEVERE));
216     }
217 
218 
219     /***
220      * Is fatal logging currently enabled?
221      */
222     public boolean isFatalEnabled() {
223         return (getLogger().isLoggable(Level.SEVERE));
224     }
225 
226 
227     /***
228      * Is info logging currently enabled?
229      */
230     public boolean isInfoEnabled() {
231         return (getLogger().isLoggable(Level.INFO));
232     }
233 
234 
235     /***
236      * Is trace logging currently enabled?
237      */
238     public boolean isTraceEnabled() {
239         return (getLogger().isLoggable(Level.FINEST));
240     }
241 
242 
243     /***
244      * Is warn logging currently enabled?
245      */
246     public boolean isWarnEnabled() {
247         return (getLogger().isLoggable(Level.WARNING));
248     }
249 
250 
251     /***
252      * Log a message with trace log level.
253      */
254     public void trace(Object message) {
255         log(Level.FINEST, String.valueOf(message), null);
256     }
257 
258 
259     /***
260      * Log a message and exception with trace log level.
261      */
262     public void trace(Object message, Throwable exception) {
263         log(Level.FINEST, String.valueOf(message), exception);
264     }
265 
266 
267     /***
268      * Log a message with warn log level.
269      */
270     public void warn(Object message) {
271         log(Level.WARNING, String.valueOf(message), null);
272     }
273 
274 
275     /***
276      * Log a message and exception with warn log level.
277      */
278     public void warn(Object message, Throwable exception) {
279         log(Level.WARNING, String.valueOf(message), exception);
280     }
281 
282 
283 }