View Javadoc

    1   // ========================================================================
    2   // Copyright 2004-2005 Mort Bay Consulting Pty. Ltd.
    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   // http://www.apache.org/licenses/LICENSE-2.0
    8   // Unless required by applicable law or agreed to in writing, software
    9   // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  // ========================================================================
    14
    15  package org.mortbay.log;
    16  import java.lang.reflect.Method;
    17  import java.security.AccessController;
    18  import java.security.PrivilegedAction;
    19
    20  import org.mortbay.util.Loader;
    21
    22
    23
    24  /*-----------------------------------------------------------------------*/
    25  /** Logging.
    26   * This class provides a static logging interface.  If an instance of the
    27   * org.slf4j.Logger class is found on the classpath, the static log methods
    28   * are directed to a slf4j logger for "org.mortbay.log".   Otherwise the logs
    29   * are directed to stderr.
    30   *
    31   * If the system property VERBOSE is set, then ignored exceptions are logged in detail.
    32   *
    33   */
    34  public class Log
    35  {
    36      private static final String[] __nestedEx =
    37          {"getTargetException","getTargetError","getException","getRootCause"};
    38      /*-------------------------------------------------------------------*/
    39      private static final Class[] __noArgs=new Class[0];
    40      public final static String EXCEPTION= "EXCEPTION ";
    41      public final static String IGNORED= "IGNORED";
    42      public final static String IGNORED_FMT= "IGNORED: {}";
    43      public final static String NOT_IMPLEMENTED= "NOT IMPLEMENTED ";
    44
    45      public static String __logClass;
    46      public static boolean __verbose;
    47      public static boolean __ignored;
    48
    49      private static Logger __log;
    50
    51      static
    52      {
    53          AccessController.doPrivileged(new PrivilegedAction()
    54              {
    55                  public Object run()
    56                  {
    57                      __logClass = System.getProperty("org.mortbay.log.class","org.mortbay.log.Slf4jLog");
    58                      __verbose = System.getProperty("VERBOSE",null)!=null;
    59                      __ignored = System.getProperty("IGNORED",null)!=null;
    60                      return new Boolean(true);
    61                  }
    62              });
    63
    64          Class log_class=null;
    65          try
    66          {
    67              log_class=Loader.loadClass(Log.class, __logClass);
    68              __log=(Logger) log_class.newInstance();
    69          }
    70          catch(Throwable e)
    71          {
    72              log_class = StdErrLog.class;
    73              __log = new StdErrLog();
    74              __logClass = log_class.getName();
    75              if(__verbose)
    76                  e.printStackTrace();
    77          }
    78
    79          __log.info("Logging to {} via {}",__log,log_class.getName());
    80      }
    81
    82      public static void setLog(Logger log)
    83      {
    84          Log.__log=log;
    85      }
    86
    87      public static Logger getLog()
    88      {
    89          return __log;
    90      }
    91
    92
    93      public static void debug(Throwable th)
    94      {
    95          if (__log==null || !isDebugEnabled())
    96              return;
    97          __log.debug(EXCEPTION,th);
    98          unwind(th);
    99      }
    100
    101     public static void debug(String msg)
    102     {
    103         if (__log==null)
    104             return;
    105         __log.debug(msg,null,null);
    106     }
    107
    108     public static void debug(String msg,Object arg)
    109     {
    110         if (__log==null)
    111             return;
    112         __log.debug(msg,arg,null);
    113     }
    114
    115     public static void debug(String msg,Object arg0, Object arg1)
    116     {
    117         if (__log==null)
    118             return;
    119         __log.debug(msg,arg0,arg1);
    120     }
    121
    122     /* ------------------------------------------------------------ */
    123     /**
    124      * Ignore an exception unless trace is enabled.
    125      * This works around the problem that log4j does not support the trace level.
    126      */
    127     public static void ignore(Throwable th)
    128     {
    129         if (__log==null)
    130             return;
    131 	if (__ignored)
    132 	{
    133             __log.warn(IGNORED,th);
    134             unwind(th);
    135 	}
    136         else if (__verbose)
    137         {
    138             __log.debug(IGNORED,th);
    139             unwind(th);
    140         }
    141     }
    142
    143     public static void info(String msg)
    144     {
    145         if (__log==null)
    146             return;
    147         __log.info(msg,null,null);
    148     }
    149
    150     public static void info(String msg,Object arg)
    151     {
    152         if (__log==null)
    153             return;
    154         __log.info(msg,arg,null);
    155     }
    156
    157     public static void info(String msg,Object arg0, Object arg1)
    158     {
    159         if (__log==null)
    160             return;
    161         __log.info(msg,arg0,arg1);
    162     }
    163
    164     public static boolean isDebugEnabled()
    165     {
    166         if (__log==null)
    167             return false;
    168         return __log.isDebugEnabled();
    169     }
    170
    171     public static void warn(String msg)
    172     {
    173         if (__log==null)
    174             return;
    175         __log.warn(msg,null,null);
    176     }
    177
    178     public static void warn(String msg,Object arg)
    179     {
    180         if (__log==null)
    181             return;
    182         __log.warn(msg,arg,null);
    183     }
    184
    185     public static void warn(String msg,Object arg0, Object arg1)
    186     {
    187         if (__log==null)
    188             return;
    189         __log.warn(msg,arg0,arg1);
    190     }
    191
    192     public static void warn(String msg, Throwable th)
    193     {
    194         if (__log==null)
    195             return;
    196         __log.warn(msg,th);
    197         unwind(th);
    198     }
    199
    200     public static void warn(Throwable th)
    201     {
    202         if (__log==null)
    203             return;
    204         __log.warn(EXCEPTION,th);
    205         unwind(th);
    206     }
    207
    208     /** Obtain a named Logger.
    209      * Obtain a named Logger or the default Logger if null is passed.
    210      */
    211     public static Logger getLogger(String name)
    212     {
    213         if (__log==null)
    214             return __log;
    215         if (name==null)
    216           return __log;
    217         return __log.getLogger(name);
    218     }
    219
    220     private static void unwind(Throwable th)
    221     {
    222         if (th==null)
    223             return;
    224         for (int i=0;i<__nestedEx.length;i++)
    225         {
    226             try
    227             {
    228                 Method get_target = th.getClass().getMethod(__nestedEx[i],__noArgs);
    229                 Throwable th2=(Throwable)get_target.invoke(th,(Object[])null);
    230                 if (th2!=null && th2!=th)
    231                     warn("Nested in "+th+":",th2);
    232             }
    233             catch(Exception ignore){}
    234         }
    235     }
    236
    237
    238 }
    239