001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     * 
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     * 
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */ 
017    
018    
019    package org.apache.commons.logging.impl;
020    
021    
022    import java.io.Serializable;
023    import java.util.logging.Level;
024    import java.util.logging.Logger;
025    import java.util.logging.LogRecord;
026    import java.util.StringTokenizer;
027    import java.io.PrintWriter;
028    import java.io.StringWriter;
029    
030    import org.apache.commons.logging.Log;
031    
032    
033    /**
034     * <p>Implementation of the <code>org.apache.commons.logging.Log</code>
035     * interface that wraps the standard JDK logging mechanisms that are
036     * available in SourceForge's Lumberjack for JDKs prior to 1.4.</p>
037     *
038     * @author <a href="mailto:sanders@apache.org">Scott Sanders</a>
039     * @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
040     * @author <a href="mailto:donaldp@apache.org">Peter Donald</a>
041     * @author <a href="mailto:vince256@comcast.net">Vince Eagen</a>
042     * @version $Revision: 424107 $ $Date: 2006-07-21 01:15:42 +0200 (fr, 21 jul 2006) $
043     * @since 1.1
044     */
045    
046    public class Jdk13LumberjackLogger implements Log, Serializable {
047    
048    
049        // ----------------------------------------------------- Instance Variables
050    
051    
052        /**
053         * The underlying Logger implementation we are using.
054         */
055        protected transient Logger logger = null;
056        protected String name = null;
057        private String sourceClassName = "unknown";
058        private String sourceMethodName = "unknown";
059        private boolean classAndMethodFound = false;
060    
061    
062        /**
063         * This member variable simply ensures that any attempt to initialise
064         * this class in a pre-1.4 JVM will result in an ExceptionInInitializerError.
065         * It must not be private, as an optimising compiler could detect that it
066         * is not used and optimise it away.
067         */
068        protected static final Level dummyLevel = Level.FINE;
069    
070        // ----------------------------------------------------------- Constructors
071    
072    
073        /**
074         * Construct a named instance of this Logger.
075         *
076         * @param name Name of the logger to be constructed
077         */
078        public Jdk13LumberjackLogger(String name) {
079    
080            this.name = name;
081            logger = getLogger();
082    
083        }
084    
085    
086        // --------------------------------------------------------- Public Methods
087    
088    
089        private void log( Level level, String msg, Throwable ex ) {
090            if( getLogger().isLoggable(level) ) {
091                LogRecord record = new LogRecord(level, msg);
092                if( !classAndMethodFound ) {
093                    getClassAndMethod();
094                }
095                record.setSourceClassName(sourceClassName);
096                record.setSourceMethodName(sourceMethodName);
097                if( ex != null ) {
098                    record.setThrown(ex);
099                }
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    }