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      StringBuffer _buffer = new StringBuffer();
    33
    34      static
    35      {
    36          try
    37          {
    38              _dateCache=new DateCache("yyyy-MM-dd HH:mm:ss");
    39          }
    40          catch(Exception e)
    41          {
    42              e.printStackTrace();
    43          }
    44
    45      }
    46
    47      public StdErrLog()
    48      {
    49          this(null);
    50      }
    51
    52      public StdErrLog(String name)
    53      {
    54          this._name=name==null?"":name;
    55      }
    56
    57      public boolean isDebugEnabled()
    58      {
    59          return __debug;
    60      }
    61
    62      public void setDebugEnabled(boolean enabled)
    63      {
    64          __debug=enabled;
    65      }
    66
    67      public void info(String msg,Object arg0, Object arg1)
    68      {
    69          String d=_dateCache.now();
    70          int ms=_dateCache.lastMs();
    71          synchronized(_buffer)
    72          {
    73              tag(d,ms,":INFO:");
    74              format(msg,arg0,arg1);
    75              System.err.println(_buffer.toString());
    76          }
    77      }
    78
    79      public void debug(String msg,Throwable th)
    80      {
    81          if (__debug)
    82          {
    83              String d=_dateCache.now();
    84              int ms=_dateCache.lastMs();
    85              synchronized(_buffer)
    86              {
    87                  tag(d,ms,":DBUG:");
    88                  format(msg);
    89                  format(th);
    90                  System.err.println(_buffer.toString());
    91              }
    92          }
    93      }
    94
    95      public void debug(String msg,Object arg0, Object arg1)
    96      {
    97          if (__debug)
    98          {
    99              String d=_dateCache.now();
    100             int ms=_dateCache.lastMs();
    101             synchronized(_buffer)
    102             {
    103                 tag(d,ms,":DBUG:");
    104                 format(msg,arg0,arg1);
    105                 System.err.println(_buffer.toString());
    106             }
    107         }
    108     }
    109
    110     public void warn(String msg,Object arg0, Object arg1)
    111     {
    112         String d=_dateCache.now();
    113         int ms=_dateCache.lastMs();
    114         synchronized(_buffer)
    115         {
    116             tag(d,ms,":WARN:");
    117             format(msg,arg0,arg1);
    118             System.err.println(_buffer.toString());
    119         }
    120     }
    121
    122     public void warn(String msg, Throwable th)
    123     {
    124         String d=_dateCache.now();
    125         int ms=_dateCache.lastMs();
    126         synchronized(_buffer)
    127         {
    128             tag(d,ms,":WARN:");
    129             format(msg);
    130             format(th);
    131             System.err.println(_buffer.toString());
    132         }
    133     }
    134
    135     private void tag(String d,int ms,String tag)
    136     {
    137         _buffer.setLength(0);
    138         _buffer.append(d);
    139         if (ms>99)
    140             _buffer.append('.');
    141         else if (ms>9)
    142             _buffer.append(".0");
    143         else
    144             _buffer.append(".00");
    145         _buffer.append(ms).append(tag).append(_name).append(':');
    146     }
    147
    148     private void format(String msg, Object arg0, Object arg1)
    149     {
    150         int i0=msg==null?-1:msg.indexOf("{}");
    151         int i1=i0<0?-1:msg.indexOf("{}",i0+2);
    152
    153         if (i0>=0)
    154         {
    155             format(msg.substring(0,i0));
    156             format(String.valueOf(arg0==null?"null":arg0));
    157
    158             if (i1>=0)
    159             {
    160                 format(msg.substring(i0+2,i1));
    161                 format(String.valueOf(arg1==null?"null":arg1));
    162                 format(msg.substring(i1+2));
    163             }
    164             else
    165             {
    166                 format(msg.substring(i0+2));
    167                 if (arg1!=null)
    168                 {
    169                     _buffer.append(' ');
    170                     format(String.valueOf(arg1));
    171                 }
    172             }
    173         }
    174         else
    175         {
    176             format(msg);
    177             if (arg0!=null)
    178             {
    179                 _buffer.append(' ');
    180                 format(String.valueOf(arg0));
    181             }
    182             if (arg1!=null)
    183             {
    184                 _buffer.append(' ');
    185                 format(String.valueOf(arg1));
    186             }
    187         }
    188     }
    189
    190     private void format(String msg)
    191     {
    192         if (msg == null)
    193             _buffer.append("null");
    194         else
    195             for (int i=0;i<msg.length();i++)
    196             {
    197                 char c=msg.charAt(i);
    198                 if (Character.isISOControl(c))
    199                 {
    200                     if (c=='\n')
    201                         _buffer.append('|');
    202                     else if (c=='\r')
    203                         _buffer.append('<');
    204                     else
    205                         _buffer.append('?');
    206                 }
    207                 else
    208                     _buffer.append(c);
    209             }
    210     }
    211
    212     private void format(Throwable th)
    213     {
    214         if (th == null)
    215             _buffer.append("null");
    216         else
    217         {
    218             _buffer.append('\n');
    219             format(th.toString());
    220             StackTraceElement[] elements = th.getStackTrace();
    221             for (int i=0;elements!=null && i<elements.length;i++)
    222             {
    223                 _buffer.append("\n\tat ");
    224                 format(elements[i].toString());
    225             }
    226         }
    227     }
    228
    229     public Logger getLogger(String name)
    230     {
    231         if ((name==null && this._name==null) ||
    232             (name!=null && name.equals(this._name)))
    233             return this;
    234         return new StdErrLog(name);
    235     }
    236
    237     public String toString()
    238     {
    239         return "STDERR"+_name;
    240     }
    241
    242
    243 }
    244