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