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
    17  import org.mortbay.util.DateCache;
    18
    19  /*-----------------------------------------------------------------------*/
    20  /** StdErr Logging.
    21   * This implementation of the Logging facade sends all logs to StdErr with minimal formatting.
    22   *
    23   * If the system property DEBUG is set, then debug logs are printed if stderr is being used.
    24   *
    25   */
    26  public class StdErrLog implements Logger
    27  {
    28      private static DateCache _dateCache;
    29      private static boolean debug = System.getProperty("DEBUG",null)!=null;
    30      private String name;
    31
    32      static
    33      {
    34          try
    35          {
    36              _dateCache=new DateCache("yyyy-MM-dd HH:mm:ss");
    37          }
    38          catch(Exception e)
    39          {
    40              e.printStackTrace();
    41          }
    42
    43      }
    44
    45      public StdErrLog()
    46      {
    47          this(null);
    48      }
    49
    50      public StdErrLog(String name)
    51      {
    52          this.name=name==null?"":name;
    53      }
    54
    55      public boolean isDebugEnabled()
    56      {
    57          return debug;
    58      }
    59
    60      public void setDebugEnabled(boolean enabled)
    61      {
    62          debug=enabled;
    63      }
    64
    65      public void info(String msg,Object arg0, Object arg1)
    66      {
    67          String d=_dateCache.now();
    68          int ms=_dateCache.lastMs();
    69          System.err.println(d+(ms>99?".":(ms>0?".0":".00"))+ms+":"+name+":INFO:  "+format(msg,arg0,arg1));
    70      }
    71
    72      public void debug(String msg,Throwable th)
    73      {
    74          if (debug)
    75          {
    76              String d=_dateCache.now();
    77              int ms=_dateCache.lastMs();
    78              System.err.println(d+(ms>99?".":(ms>0?".0":".00"))+ms+":"+name+":DEBUG: "+msg);
    79              if (th!=null) th.printStackTrace();
    80          }
    81      }
    82
    83      public void debug(String msg,Object arg0, Object arg1)
    84      {
    85          if (debug)
    86          {
    87              String d=_dateCache.now();
    88              int ms=_dateCache.lastMs();
    89              System.err.println(d+(ms>99?".":(ms>0?".0":".00"))+ms+":"+name+":DEBUG: "+format(msg,arg0,arg1));
    90          }
    91      }
    92
    93      public void warn(String msg,Object arg0, Object arg1)
    94      {
    95          String d=_dateCache.now();
    96          int ms=_dateCache.lastMs();
    97          System.err.println(d+(ms>99?".":(ms>0?".0":".00"))+ms+":"+name+":WARN:  "+format(msg,arg0,arg1));
    98      }
    99
    100     public void warn(String msg, Throwable th)
    101     {
    102         String d=_dateCache.now();
    103         int ms=_dateCache.lastMs();
    104         System.err.println(d+(ms>99?".":(ms>0?".0":".00"))+ms+":"+name+":WARN:  "+msg);
    105         if (th!=null)
    106             th.printStackTrace();
    107     }
    108
    109     private String format(String msg, Object arg0, Object arg1)
    110     {
    111         int i0=msg.indexOf("{}");
    112         int i1=i0<0?-1:msg.indexOf("{}",i0+2);
    113
    114         if (arg1!=null && i1>=0)
    115             msg=msg.substring(0,i1)+arg1+msg.substring(i1+2);
    116         if (arg0!=null && i0>=0)
    117             msg=msg.substring(0,i0)+arg0+msg.substring(i0+2);
    118         return msg;
    119     }
    120
    121     public Logger getLogger(String name)
    122     {
    123         if ((name==null && this.name==null) ||
    124             (name!=null && name.equals(this.name)))
    125             return this;
    126         return new StdErrLog(name);
    127     }
    128
    129     public String toString()
    130     {
    131         return "STDERR"+name;
    132     }
    133
    134 }
    135