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