View Javadoc

1   // ========================================================================
2   // Copyright (c) 2004-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  
14  package org.eclipse.jetty.util.log;
15  
16  import org.eclipse.jetty.util.DateCache;
17  
18  /*-----------------------------------------------------------------------*/
19  /** StdErr Logging.
20   * This implementation of the Logging facade sends all logs to StdErr with minimal formatting.
21   * 
22   * If the system property org.eclipse.jetty.util.log.DEBUG is set, 
23   * 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      
30      private static boolean _debug = Boolean.parseBoolean(System.getProperty("org.eclipse.jetty.util.log.DEBUG","false"));
31      private String _name;
32      private boolean _hideStacks=false;
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 boolean isHideStacks()
68      {
69          return _hideStacks;
70      }
71  
72      public void setHideStacks(boolean hideStacks)
73      {
74          _hideStacks = hideStacks;
75      }
76  
77      public void info(String msg,Object arg0, Object arg1)
78      {
79          String d=_dateCache.now();
80          int ms=_dateCache.lastMs();
81          System.err.println(d+(ms>99?".":(ms>0?".0":".00"))+ms+":"+_name+":INFO:  "+format(msg,arg0,arg1));
82      }
83      
84      public void debug(String msg,Throwable th)
85      {
86          if (_debug)
87          {
88              String d=_dateCache.now();
89              int ms=_dateCache.lastMs();
90              System.err.println(d+(ms>99?".":(ms>0?".0":".00"))+ms+":"+_name+":DEBUG: "+msg);
91              if (th!=null) 
92              {
93                  if (_hideStacks)
94                      System.err.println(th);
95                  else
96                      th.printStackTrace();
97              }
98          }
99      }
100     
101     public void debug(String msg,Object arg0, Object arg1)
102     {
103         if (_debug)
104         {
105             String d=_dateCache.now();
106             int ms=_dateCache.lastMs();
107             System.err.println(d+(ms>99?".":(ms>0?".0":".00"))+ms+":"+_name+":DEBUG: "+format(msg,arg0,arg1));
108         }
109     }
110     
111     public void warn(String msg,Object arg0, Object arg1)
112     {
113         String d=_dateCache.now();
114         int ms=_dateCache.lastMs();
115         System.err.println(d+(ms>99?".":(ms>0?".0":".00"))+ms+":"+_name+":WARN:  "+format(msg,arg0,arg1));
116     }
117     
118     public void warn(String msg, Throwable th)
119     {
120         String d=_dateCache.now();
121         int ms=_dateCache.lastMs();
122         System.err.println(d+(ms>99?".":(ms>0?".0":".00"))+ms+":"+_name+":WARN:  "+msg);
123         if (th!=null) 
124         {
125             if (_hideStacks)
126                 System.err.println(th);
127             else
128                 th.printStackTrace();
129         }
130     }
131 
132     private String format(String msg, Object arg0, Object arg1)
133     {
134         int i0=msg.indexOf("{}");
135         int i1=i0<0?-1:msg.indexOf("{}",i0+2);
136         
137         if (arg1!=null && i1>=0)
138             msg=msg.substring(0,i1)+arg1+msg.substring(i1+2);
139         if (arg0!=null && i0>=0)
140             msg=msg.substring(0,i0)+arg0+msg.substring(i0+2);
141         return msg;
142     }
143     
144     public Logger getLogger(String name)
145     {
146         if ((name==null && this._name==null) ||
147             (name!=null && name.equals(this._name)))
148             return this;
149         return new StdErrLog(name);
150     }
151     
152     public String toString()
153     {
154         return "STDERR"+_name;
155     }
156 
157 }
158