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 java.security.AccessControlException;
17  
18  import org.eclipse.jetty.util.DateCache;
19  
20  /*-----------------------------------------------------------------------*/
21  /** StdErr Logging.
22   * This implementation of the Logging facade sends all logs to StdErr with minimal formatting.
23   * 
24   * If the system property "org.eclipse.jetty.util.log.DEBUG" is set, 
25   * then debug logs are printed if stderr is being used.
26   * <p>
27   * For named debuggers, the system property name+".DEBUG" is checked. If it is not not set, then
28   * "org.eclipse.jetty.util.log.DEBUG" is used as the default.
29   * 
30   */
31  public class StdErrLog implements Logger
32  {    
33      private static DateCache _dateCache;
34      
35      private final static String LN = System.getProperty("line.separator");
36      private final static boolean __debug = 
37          Boolean.parseBoolean(System.getProperty("org.eclipse.jetty.util.log.DEBUG",System.getProperty("org.eclipse.jetty.util.log.stderr.DEBUG","false")));
38      private boolean _debug = __debug;
39      private final String _name;
40      private boolean _hideStacks=false;
41      StringBuilder _buffer = new StringBuilder();
42      
43      static
44      {
45          try
46          {
47              _dateCache=new DateCache("yyyy-MM-dd HH:mm:ss");
48          }
49          catch(Exception e)
50          {
51              e.printStackTrace();
52          }
53          
54      }
55      
56      public StdErrLog()
57      {
58          this(null);
59      }
60      
61      public StdErrLog(String name)
62      {    
63          this._name=name==null?"":name;
64  
65          try
66          {
67              _debug = Boolean.parseBoolean(System.getProperty(_name + ".DEBUG",Boolean.toString(__debug)));
68          }
69          catch (AccessControlException ace)
70          {
71              _debug = __debug;
72          }
73      }
74      
75      public String getName()
76      {
77          return _name;
78      }
79      
80      public boolean isDebugEnabled()
81      {
82          return _debug;
83      }
84      
85      public void setDebugEnabled(boolean enabled)
86      {
87          _debug=enabled;
88      }
89      
90      public boolean isHideStacks()
91      {
92          return _hideStacks;
93      }
94  
95      public void setHideStacks(boolean hideStacks)
96      {
97          _hideStacks = hideStacks;
98      }
99  
100     public void info(String msg)
101     {
102         String d=_dateCache.now();
103         int ms=_dateCache.lastMs();
104         synchronized(_buffer)
105         {
106             tag(d,ms,":INFO:");
107             format(msg);
108             System.err.println(_buffer.toString());
109         }
110     }
111 
112     public void info(String msg,Object arg0, Object arg1)
113     {
114         String d=_dateCache.now();
115         int ms=_dateCache.lastMs();
116         synchronized(_buffer)
117         {
118             tag(d,ms,":INFO:");
119             format(msg,arg0,arg1);
120             System.err.println(_buffer.toString());
121         }
122     }
123     
124     public void debug(String msg,Throwable th)
125     {
126         if (_debug)
127         {
128             String d=_dateCache.now();
129             int ms=_dateCache.lastMs();
130             synchronized(_buffer)
131             {
132                 tag(d,ms,":DBUG:");
133                 format(msg);
134                 if (_hideStacks)
135                     format(th.toString());
136                 else
137                     format(th);
138                 System.err.println(_buffer.toString());
139             }
140         }
141     }
142     
143     public void debug(String msg)
144     {
145         if (_debug)
146         {
147             String d=_dateCache.now();
148             int ms=_dateCache.lastMs();
149             synchronized(_buffer)
150             {
151                 tag(d,ms,":DBUG:");
152                 format(msg);
153                 System.err.println(_buffer.toString());
154             }
155         }
156     }
157     
158     public void debug(String msg,Object arg0, Object arg1)
159     {
160         if (_debug)
161         {
162             String d=_dateCache.now();
163             int ms=_dateCache.lastMs();
164             synchronized(_buffer)
165             {
166                 tag(d,ms,":DBUG:");
167                 format(msg,arg0,arg1);
168                 System.err.println(_buffer.toString());
169             }
170         }
171     }
172     
173     public void warn(String msg)
174     {
175         String d=_dateCache.now();
176         int ms=_dateCache.lastMs();
177         synchronized(_buffer)
178         {
179             tag(d,ms,":WARN:");
180             format(msg);
181             System.err.println(_buffer.toString());
182         }
183     }
184     
185     public void warn(String msg,Object arg0, Object arg1)
186     {
187         String d=_dateCache.now();
188         int ms=_dateCache.lastMs();
189         synchronized(_buffer)
190         {
191             tag(d,ms,":WARN:");
192             format(msg,arg0,arg1);
193             System.err.println(_buffer.toString());
194         }
195     }
196     
197     public void warn(String msg, Throwable th)
198     {
199         String d=_dateCache.now();
200         int ms=_dateCache.lastMs();  
201         synchronized(_buffer)
202         {
203             tag(d,ms,":WARN:");
204             format(msg);
205             if (_hideStacks)
206                 format(th.toString());
207             else
208                 format(th);
209             System.err.println(_buffer.toString());
210         }    
211     }
212 
213     
214     private void tag(String d,int ms,String tag)
215     {
216         _buffer.setLength(0);
217         _buffer.append(d);
218         if (ms>99)
219             _buffer.append('.');
220         else if (ms>9)
221             _buffer.append(".0");
222         else
223             _buffer.append(".00");
224         _buffer.append(ms).append(tag).append(_name).append(':');
225     }
226     
227     private void format(String msg, Object arg0, Object arg1)
228     {
229         int i0=msg==null?-1:msg.indexOf("{}");
230         int i1=i0<0?-1:msg.indexOf("{}",i0+2);
231         
232         if (i0>=0)
233         {
234             format(msg.substring(0,i0));
235             format(String.valueOf(arg0==null?"null":arg0));
236             
237             if (i1>=0)
238             {
239                 format(msg.substring(i0+2,i1));
240                 format(String.valueOf(arg1==null?"null":arg1));
241                 format(msg.substring(i1+2));
242             }
243             else
244             {
245                 format(msg.substring(i0+2));
246                 if (arg1!=null)
247                 {
248                     _buffer.append(' ');
249                     format(String.valueOf(arg1));
250                 }
251             }
252         }
253         else
254         {
255             format(msg);
256             if (arg0!=null)
257             {
258                 _buffer.append(' ');
259                 format(String.valueOf(arg0));
260             }
261             if (arg1!=null)
262             {
263                 _buffer.append(' ');
264                 format(String.valueOf(arg1));
265             }
266         }
267     }
268     
269     private void format(String msg)
270     {
271         if (msg == null)
272             _buffer.append("null");
273         else
274             for (int i=0;i<msg.length();i++)
275             {
276                 char c=msg.charAt(i);
277                 if (Character.isISOControl(c))
278                 {
279                     if (c=='\n')
280                         _buffer.append('|');
281                     else if (c=='\r')
282                         _buffer.append('<');
283                     else
284                         _buffer.append('?');
285                 }
286                 else
287                     _buffer.append(c);
288             }
289     }
290     
291     private void format(Throwable th)
292     {
293         if (th == null)
294             _buffer.append("null");
295         else
296         {
297             _buffer.append('\n');
298             format(th.toString());
299             StackTraceElement[] elements = th.getStackTrace();
300             for (int i=0;elements!=null && i<elements.length;i++)
301             {
302                 _buffer.append("\n\tat ");
303                 format(elements[i].toString());
304             }
305         }
306     }
307     
308     public Logger getLogger(String name)
309     {
310         if ((name==null && this._name==null) ||
311             (name!=null && name.equals(this._name)))
312             return this;
313         return new StdErrLog(_name==null||_name.length()==0?name:_name+"."+name);
314     }
315     
316     @Override
317     public String toString()
318     {
319         return "StdErrLog:"+_name+":DEBUG="+_debug;
320     }
321 
322 }
323