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