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      
41      static
42      {
43          try
44          {
45              _dateCache=new DateCache("yyyy-MM-dd HH:mm:ss");
46          }
47          catch(Exception e)
48          {
49              e.printStackTrace();
50          }
51          
52      }
53      
54      public StdErrLog()
55      {
56          this(null);
57      }
58      
59      public StdErrLog(String name)
60      {    
61          this._name=name==null?"":name;
62  
63          try
64          {
65              _debug = Boolean.parseBoolean(System.getProperty(name + ".DEBUG",Boolean.toString(__debug)));
66          }
67          catch (AccessControlException ace)
68          {
69              _debug = __debug;
70          }
71      }
72      
73      public String getName()
74      {
75          return _name;
76      }
77      
78      public boolean isDebugEnabled()
79      {
80          return _debug;
81      }
82      
83      public void setDebugEnabled(boolean enabled)
84      {
85          _debug=enabled;
86      }
87      
88      public boolean isHideStacks()
89      {
90          return _hideStacks;
91      }
92  
93      public void setHideStacks(boolean hideStacks)
94      {
95          _hideStacks = hideStacks;
96      }
97  
98      public void info(String msg)
99      {
100         String d=_dateCache.now();
101         int ms=_dateCache.lastMs();
102         System.err.println(d+(ms>99?".":(ms>0?".0":".00"))+ms+":INFO:"+_name+":"+msg);
103     }
104 
105     public void info(String msg,Object arg0, Object arg1)
106     {
107         String d=_dateCache.now();
108         int ms=_dateCache.lastMs();
109         System.err.println(d+(ms>99?".":(ms>0?".0":".00"))+ms+":INFO:"+_name+":"+format(msg,arg0,arg1));
110     }
111     
112     public void debug(String msg,Throwable th)
113     {
114         if (_debug)
115         {
116             String d=_dateCache.now();
117             int ms=_dateCache.lastMs();
118             System.err.println(d+(ms>99?".":(ms>0?".0":".00"))+ms+":DBUG:"+_name+":"+msg);
119             if (th!=null) 
120             {
121                 if (_hideStacks)
122                     System.err.println(th);
123                 else
124                     th.printStackTrace();
125             }
126         }
127     }
128     
129     public void debug(String msg)
130     {
131         if (_debug)
132         {
133             String d=_dateCache.now();
134             int ms=_dateCache.lastMs();
135             System.err.println(d+(ms>99?".":(ms>0?".0":".00"))+ms+":DBUG:"+_name+":"+msg);
136         }
137     }
138     
139     public void debug(String msg,Object arg0, Object arg1)
140     {
141         if (_debug)
142         {
143             String d=_dateCache.now();
144             int ms=_dateCache.lastMs();
145             System.err.println(d+(ms>99?".":(ms>0?".0":".00"))+ms+":DBUG:"+_name+":"+format(msg,arg0,arg1));
146         }
147     }
148     
149     public void warn(String msg)
150     {
151         String d=_dateCache.now();
152         int ms=_dateCache.lastMs();
153         System.err.println(d+(ms>99?".":(ms>0?".0":".00"))+ms+":WARN:"+_name+":"+msg);
154     }
155     
156     public void warn(String msg,Object arg0, Object arg1)
157     {
158         String d=_dateCache.now();
159         int ms=_dateCache.lastMs();
160         System.err.println(d+(ms>99?".":(ms>0?".0":".00"))+ms+":WARN:"+_name+":"+format(msg,arg0,arg1));
161     }
162     
163     public void warn(String msg, Throwable th)
164     {
165         String d=_dateCache.now();
166         int ms=_dateCache.lastMs();
167         System.err.println(d+(ms>99?".":(ms>0?".0":".00"))+ms+":WARN:"+_name+":"+msg);
168         if (th!=null) 
169         {
170             if (_hideStacks)
171                 System.err.println(th);
172             else
173                 th.printStackTrace();
174         }
175     }
176 
177     private String format(String msg, Object arg0, Object arg1)
178     {
179         int i0=msg.indexOf("{}");
180         int i1=i0<0?-1:msg.indexOf("{}",i0+2);
181         
182         if (arg1!=null && i1>=0)
183             msg=msg.substring(0,i1)+arg1+msg.substring(i1+2);
184         if (arg0!=null && i0>=0)
185             msg=msg.substring(0,i0)+arg0+msg.substring(i0+2);
186         return msg;
187     }
188     
189     public Logger getLogger(String name)
190     {
191         if ((name==null && this._name==null) ||
192             (name!=null && name.equals(this._name)))
193             return this;
194         return new StdErrLog(_name==null||_name.length()==0?name:_name+"."+name);
195     }
196     
197     @Override
198     public String toString()
199     {
200         return "StdErrLog:"+_name+":DEBUG="+_debug;
201     }
202 
203 }
204