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

Save This Page
Home » slf4j-1.5.5 » org.apache.commons » logging » impl » [javadoc | source]