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   * <p>
25   * For named debuggers, the system property name+".DEBUG" is checked. If it is not not set, then
26   * "org.eclipse.jetty.util.log.DEBUG" is used as the default.
27   * 
28   */
29  public class StdErrLog implements Logger
30  {    
31      private static DateCache _dateCache;
32      
33      private final static boolean __debug = Boolean.parseBoolean(System.getProperty("org.eclipse.jetty.util.log.DEBUG","false"));
34      private boolean _debug = __debug;
35      private String _name;
36      private boolean _hideStacks=false;
37      
38      static
39      {
40          try
41          {
42              _dateCache=new DateCache("yyyy-MM-dd HH:mm:ss");
43          }
44          catch(Exception e)
45          {
46              e.printStackTrace();
47          }
48          
49      }
50      
51      public StdErrLog()
52      {
53          this(null);
54      }
55      
56      public StdErrLog(String name)
57      {    
58          this._name=name==null?"":name;
59          _debug=Boolean.parseBoolean(System.getProperty(name+".DEBUG",Boolean.toString(__debug)));
60      }
61      
62      public boolean isDebugEnabled()
63      {
64          return _debug;
65      }
66      
67      public void setDebugEnabled(boolean enabled)
68      {
69          _debug=enabled;
70      }
71      
72      public boolean isHideStacks()
73      {
74          return _hideStacks;
75      }
76  
77      public void setHideStacks(boolean hideStacks)
78      {
79          _hideStacks = hideStacks;
80      }
81  
82      public void info(String msg)
83      {
84          String d=_dateCache.now();
85          int ms=_dateCache.lastMs();
86          System.err.println(d+(ms>99?".":(ms>0?".0":".00"))+ms+":"+_name+":INFO:  "+msg);
87      }
88  
89      public void info(String msg,Object arg0, Object arg1)
90      {
91          String d=_dateCache.now();
92          int ms=_dateCache.lastMs();
93          System.err.println(d+(ms>99?".":(ms>0?".0":".00"))+ms+":"+_name+":INFO:  "+format(msg,arg0,arg1));
94      }
95      
96      public void debug(String msg,Throwable th)
97      {
98          if (_debug)
99          {
100             String d=_dateCache.now();
101             int ms=_dateCache.lastMs();
102             System.err.println(d+(ms>99?".":(ms>0?".0":".00"))+ms+":"+_name+":DEBUG: "+msg);
103             if (th!=null) 
104             {
105                 if (_hideStacks)
106                     System.err.println(th);
107                 else
108                     th.printStackTrace();
109             }
110         }
111     }
112     
113     public void debug(String msg)
114     {
115         if (_debug)
116         {
117             String d=_dateCache.now();
118             int ms=_dateCache.lastMs();
119             System.err.println(d+(ms>99?".":(ms>0?".0":".00"))+ms+":"+_name+":DEBUG: "+msg);
120         }
121     }
122     
123     public void debug(String msg,Object arg0, Object arg1)
124     {
125         if (_debug)
126         {
127             String d=_dateCache.now();
128             int ms=_dateCache.lastMs();
129             System.err.println(d+(ms>99?".":(ms>0?".0":".00"))+ms+":"+_name+":DEBUG: "+format(msg,arg0,arg1));
130         }
131     }
132     
133     public void warn(String msg)
134     {
135         String d=_dateCache.now();
136         int ms=_dateCache.lastMs();
137         System.err.println(d+(ms>99?".":(ms>0?".0":".00"))+ms+":"+_name+":WARN:  "+msg);
138     }
139     
140     public void warn(String msg,Object arg0, Object arg1)
141     {
142         String d=_dateCache.now();
143         int ms=_dateCache.lastMs();
144         System.err.println(d+(ms>99?".":(ms>0?".0":".00"))+ms+":"+_name+":WARN:  "+format(msg,arg0,arg1));
145     }
146     
147     public void warn(String msg, Throwable th)
148     {
149         String d=_dateCache.now();
150         int ms=_dateCache.lastMs();
151         System.err.println(d+(ms>99?".":(ms>0?".0":".00"))+ms+":"+_name+":WARN:  "+msg);
152         if (th!=null) 
153         {
154             if (_hideStacks)
155                 System.err.println(th);
156             else
157                 th.printStackTrace();
158         }
159     }
160 
161     private String format(String msg, Object arg0, Object arg1)
162     {
163         int i0=msg.indexOf("{}");
164         int i1=i0<0?-1:msg.indexOf("{}",i0+2);
165         
166         if (arg1!=null && i1>=0)
167             msg=msg.substring(0,i1)+arg1+msg.substring(i1+2);
168         if (arg0!=null && i0>=0)
169             msg=msg.substring(0,i0)+arg0+msg.substring(i0+2);
170         return msg;
171     }
172     
173     public Logger getLogger(String name)
174     {
175         if ((name==null && this._name==null) ||
176             (name!=null && name.equals(this._name)))
177             return this;
178         return new StdErrLog(_name==null||_name.length()==0?name:_name+"."+name);
179     }
180     
181     public String toString()
182     {
183         return "StdErrLog:"+_name+":DEBUG="+_debug;
184     }
185 
186 }
187