1   // ========================================================================
2   // Copyright 2004-2005 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // Licensed under the Apache License, Version 2.0 (the "License");
5   // you may not use this file except in compliance with the License.
6   // You may obtain a copy of the License at 
7   // http://www.apache.org/licenses/LICENSE-2.0
8   // Unless required by applicable law or agreed to in writing, software
9   // distributed under the License is distributed on an "AS IS" BASIS,
10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11  // See the License for the specific language governing permissions and
12  // limitations under the License.
13  // ========================================================================
14  
15  package org.mortbay.log;
16  
17  import org.mortbay.util.DateCache;
18  
19  /*-----------------------------------------------------------------------*/
20  /** StdErr Logging.
21   * This implementation of the Logging facade sends all logs to StdErr with minimal formatting.
22   * 
23   * If the system property DEBUG is set, then debug logs are printed if stderr is being used.
24   * 
25   */
26  public class StdErrLog implements Logger
27  {    
28      private static DateCache _dateCache;
29      private static boolean _debug = System.getProperty("DEBUG",null)!=null;
30      private String _name;
31      private boolean _hideStacks=false;
32      
33      static
34      {
35          try
36          {
37              _dateCache=new DateCache("yyyy-MM-dd HH:mm:ss");
38          }
39          catch(Exception e)
40          {
41              e.printStackTrace();
42          }
43          
44      }
45      
46      public StdErrLog()
47      {
48          this(null);
49      }
50      
51      public StdErrLog(String name)
52      {    
53          this._name=name==null?"":name;
54      }
55      
56      public boolean isDebugEnabled()
57      {
58          return _debug;
59      }
60      
61      public void setDebugEnabled(boolean enabled)
62      {
63          _debug=enabled;
64      }
65      
66      public boolean isHideStacks()
67      {
68          return _hideStacks;
69      }
70  
71      public void setHideStacks(boolean hideStacks)
72      {
73          _hideStacks = hideStacks;
74      }
75  
76      public void info(String msg,Object arg0, Object arg1)
77      {
78          String d=_dateCache.now();
79          int ms=_dateCache.lastMs();
80          System.err.println(d+(ms>99?".":(ms>0?".0":".00"))+ms+":"+_name+":INFO:  "+format(msg,arg0,arg1));
81      }
82      
83      public void debug(String msg,Throwable th)
84      {
85          if (_debug)
86          {
87              String d=_dateCache.now();
88              int ms=_dateCache.lastMs();
89              System.err.println(d+(ms>99?".":(ms>0?".0":".00"))+ms+":"+_name+":DEBUG: "+msg);
90              if (th!=null) 
91              {
92                  if (_hideStacks)
93                      System.err.println(th);
94                  else
95                      th.printStackTrace();
96              }
97          }
98      }
99      
100     public void debug(String msg,Object arg0, Object arg1)
101     {
102         if (_debug)
103         {
104             String d=_dateCache.now();
105             int ms=_dateCache.lastMs();
106             System.err.println(d+(ms>99?".":(ms>0?".0":".00"))+ms+":"+_name+":DEBUG: "+format(msg,arg0,arg1));
107         }
108     }
109     
110     public void warn(String msg,Object arg0, Object arg1)
111     {
112         String d=_dateCache.now();
113         int ms=_dateCache.lastMs();
114         System.err.println(d+(ms>99?".":(ms>0?".0":".00"))+ms+":"+_name+":WARN:  "+format(msg,arg0,arg1));
115     }
116     
117     public void warn(String msg, Throwable th)
118     {
119         String d=_dateCache.now();
120         int ms=_dateCache.lastMs();
121         System.err.println(d+(ms>99?".":(ms>0?".0":".00"))+ms+":"+_name+":WARN:  "+msg);
122         if (th!=null) 
123         {
124             if (_hideStacks)
125                 System.err.println(th);
126             else
127                 th.printStackTrace();
128         }
129     }
130 
131     private String format(String msg, Object arg0, Object arg1)
132     {
133         int i0=msg.indexOf("{}");
134         int i1=i0<0?-1:msg.indexOf("{}",i0+2);
135         
136         if (arg1!=null && i1>=0)
137             msg=msg.substring(0,i1)+arg1+msg.substring(i1+2);
138         if (arg0!=null && i0>=0)
139             msg=msg.substring(0,i0)+arg0+msg.substring(i0+2);
140         return msg;
141     }
142     
143     public Logger getLogger(String name)
144     {
145         if ((name==null && this._name==null) ||
146             (name!=null && name.equals(this._name)))
147             return this;
148         return new StdErrLog(name);
149     }
150     
151     public String toString()
152     {
153         return "STDERR"+_name;
154     }
155 
156 }
157