Home | History | Annotate | Download | only in impl
      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 
     25 import org.apache.commons.logging.Log;
     26 
     27 
     28 /**
     29  * <p>Implementation of the <code>org.apache.commons.logging.Log</code>
     30  * interface that wraps the standard JDK logging mechanisms that were
     31  * introduced in the Merlin release (JDK 1.4).</p>
     32  *
     33  * @author <a href="mailto:sanders (at) apache.org">Scott Sanders</a>
     34  * @author <a href="mailto:bloritsch (at) apache.org">Berin Loritsch</a>
     35  * @author <a href="mailto:donaldp (at) apache.org">Peter Donald</a>
     36  * @version $Revision: 370652 $ $Date: 2006-01-19 22:23:48 +0000 (Thu, 19 Jan 2006) $
     37  */
     38 
     39 public class Jdk14Logger implements Log, Serializable {
     40 
     41     /**
     42      * This member variable simply ensures that any attempt to initialise
     43      * this class in a pre-1.4 JVM will result in an ExceptionInInitializerError.
     44      * It must not be private, as an optimising compiler could detect that it
     45      * is not used and optimise it away.
     46      */
     47     protected static final Level dummyLevel = Level.FINE;
     48 
     49     // ----------------------------------------------------------- Constructors
     50 
     51 
     52     /**
     53      * Construct a named instance of this Logger.
     54      *
     55      * @param name Name of the logger to be constructed
     56      */
     57     public Jdk14Logger(String name) {
     58 
     59         this.name = name;
     60         logger = getLogger();
     61 
     62     }
     63 
     64 
     65     // ----------------------------------------------------- Instance Variables
     66 
     67 
     68     /**
     69      * The underlying Logger implementation we are using.
     70      */
     71     protected transient Logger logger = null;
     72 
     73 
     74     /**
     75      * The name of the logger we are wrapping.
     76      */
     77     protected String name = null;
     78 
     79 
     80     // --------------------------------------------------------- Public Methods
     81 
     82     private void log( Level level, String msg, Throwable ex ) {
     83 
     84         Logger logger = getLogger();
     85         if (logger.isLoggable(level)) {
     86             // Hack (?) to get the stack trace.
     87             Throwable dummyException=new Throwable();
     88             StackTraceElement locations[]=dummyException.getStackTrace();
     89             // Caller will be the third element
     90             String cname="unknown";
     91             String method="unknown";
     92             if( locations!=null && locations.length >2 ) {
     93                 StackTraceElement caller=locations[2];
     94                 cname=caller.getClassName();
     95                 method=caller.getMethodName();
     96             }
     97             if( ex==null ) {
     98                 logger.logp( level, cname, method, msg );
     99             } else {
    100                 logger.logp( level, cname, method, msg, ex );
    101             }
    102         }
    103 
    104     }
    105 
    106     /**
    107      * Logs a message with <code>java.util.logging.Level.FINE</code>.
    108      *
    109      * @param message to log
    110      * @see org.apache.commons.logging.Log#debug(Object)
    111      */
    112     public void debug(Object message) {
    113         log(Level.FINE, String.valueOf(message), null);
    114     }
    115 
    116 
    117     /**
    118      * Logs a message with <code>java.util.logging.Level.FINE</code>.
    119      *
    120      * @param message to log
    121      * @param exception log this cause
    122      * @see org.apache.commons.logging.Log#debug(Object, Throwable)
    123      */
    124     public void debug(Object message, Throwable exception) {
    125         log(Level.FINE, String.valueOf(message), exception);
    126     }
    127 
    128 
    129     /**
    130      * Logs a message with <code>java.util.logging.Level.SEVERE</code>.
    131      *
    132      * @param message to log
    133      * @see org.apache.commons.logging.Log#error(Object)
    134      */
    135     public void error(Object message) {
    136         log(Level.SEVERE, String.valueOf(message), null);
    137     }
    138 
    139 
    140     /**
    141      * Logs a message with <code>java.util.logging.Level.SEVERE</code>.
    142      *
    143      * @param message to log
    144      * @param exception log this cause
    145      * @see org.apache.commons.logging.Log#error(Object, Throwable)
    146      */
    147     public void error(Object message, Throwable exception) {
    148         log(Level.SEVERE, String.valueOf(message), exception);
    149     }
    150 
    151 
    152     /**
    153      * Logs a message with <code>java.util.logging.Level.SEVERE</code>.
    154      *
    155      * @param message to log
    156      * @see org.apache.commons.logging.Log#fatal(Object)
    157      */
    158     public void fatal(Object message) {
    159         log(Level.SEVERE, String.valueOf(message), null);
    160     }
    161 
    162 
    163     /**
    164      * Logs a message with <code>java.util.logging.Level.SEVERE</code>.
    165      *
    166      * @param message to log
    167      * @param exception log this cause
    168      * @see org.apache.commons.logging.Log#fatal(Object, Throwable)
    169      */
    170     public void fatal(Object message, Throwable exception) {
    171         log(Level.SEVERE, String.valueOf(message), exception);
    172     }
    173 
    174 
    175     /**
    176      * Return the native Logger instance we are using.
    177      */
    178     public Logger getLogger() {
    179         if (logger == null) {
    180             logger = Logger.getLogger(name);
    181         }
    182         return (logger);
    183     }
    184 
    185 
    186     /**
    187      * Logs a message with <code>java.util.logging.Level.INFO</code>.
    188      *
    189      * @param message to log
    190      * @see org.apache.commons.logging.Log#info(Object)
    191      */
    192     public void info(Object message) {
    193         log(Level.INFO, String.valueOf(message), null);
    194     }
    195 
    196 
    197     /**
    198      * Logs a message with <code>java.util.logging.Level.INFO</code>.
    199      *
    200      * @param message to log
    201      * @param exception log this cause
    202      * @see org.apache.commons.logging.Log#info(Object, Throwable)
    203      */
    204     public void info(Object message, Throwable exception) {
    205         log(Level.INFO, String.valueOf(message), exception);
    206     }
    207 
    208 
    209     /**
    210      * Is debug logging currently enabled?
    211      */
    212     public boolean isDebugEnabled() {
    213         return (getLogger().isLoggable(Level.FINE));
    214     }
    215 
    216 
    217     /**
    218      * Is error logging currently enabled?
    219      */
    220     public boolean isErrorEnabled() {
    221         return (getLogger().isLoggable(Level.SEVERE));
    222     }
    223 
    224 
    225     /**
    226      * Is fatal logging currently enabled?
    227      */
    228     public boolean isFatalEnabled() {
    229         return (getLogger().isLoggable(Level.SEVERE));
    230     }
    231 
    232 
    233     /**
    234      * Is info logging currently enabled?
    235      */
    236     public boolean isInfoEnabled() {
    237         return (getLogger().isLoggable(Level.INFO));
    238     }
    239 
    240 
    241     /**
    242      * Is trace logging currently enabled?
    243      */
    244     public boolean isTraceEnabled() {
    245         return (getLogger().isLoggable(Level.FINEST));
    246     }
    247 
    248 
    249     /**
    250      * Is warn logging currently enabled?
    251      */
    252     public boolean isWarnEnabled() {
    253         return (getLogger().isLoggable(Level.WARNING));
    254     }
    255 
    256 
    257     /**
    258      * Logs a message with <code>java.util.logging.Level.FINEST</code>.
    259      *
    260      * @param message to log
    261      * @see org.apache.commons.logging.Log#trace(Object)
    262      */
    263     public void trace(Object message) {
    264         log(Level.FINEST, String.valueOf(message), null);
    265     }
    266 
    267 
    268     /**
    269      * Logs a message with <code>java.util.logging.Level.FINEST</code>.
    270      *
    271      * @param message to log
    272      * @param exception log this cause
    273      * @see org.apache.commons.logging.Log#trace(Object, Throwable)
    274      */
    275     public void trace(Object message, Throwable exception) {
    276         log(Level.FINEST, String.valueOf(message), exception);
    277     }
    278 
    279 
    280     /**
    281      * Logs a message with <code>java.util.logging.Level.WARNING</code>.
    282      *
    283      * @param message to log
    284      * @see org.apache.commons.logging.Log#warn(Object)
    285      */
    286     public void warn(Object message) {
    287         log(Level.WARNING, String.valueOf(message), null);
    288     }
    289 
    290 
    291     /**
    292      * Logs a message with <code>java.util.logging.Level.WARNING</code>.
    293      *
    294      * @param message to log
    295      * @param exception log this cause
    296      * @see org.apache.commons.logging.Log#warn(Object, Throwable)
    297      */
    298     public void warn(Object message, Throwable exception) {
    299         log(Level.WARNING, String.valueOf(message), exception);
    300     }
    301 
    302 
    303 }
    304