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