View Javadoc

1   /*
2    * Copyright 2003 The Apache Software Foundation.
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    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.velocity.tools.test;
18  
19  import java.io.PrintWriter;
20  import java.io.IOException;
21  
22  import org.mortbay.log.Logger;
23  import org.mortbay.util.DateCache;
24  
25  /** Basic Jetty logger for our showcase webapp
26   *
27   *  @author <a href=mailto:cbrisson@apache.org>Claude Brisson</a>
28   */
29  
30  public class JettyLogger implements Logger {
31  
32      private boolean debug = false;
33      private String name = null;
34      private DateCache _dateCache=new DateCache("yyyy-MM-dd HH:mm:ss.SSS");
35  
36      private static PrintWriter out = null;
37  
38      static {
39          try {
40              String logfile = System.getProperty("jetty.log.file","/tmp/error.log");
41              out = new PrintWriter(logfile);
42          } catch(IOException ioe) {
43              System.out.println(ioe.getMessage());
44          }
45      }
46  
47      public JettyLogger() {
48          this(null);
49      }
50  
51      public JettyLogger(String name) {
52          this.name = name==null? "" : name;
53      }
54  
55      /*
56       * org.mortbay.log.Logger interface
57       */
58  
59      public boolean isDebugEnabled() {
60          return debug;
61      }
62  
63      public void setDebugEnabled(boolean enabled) {
64          debug = enabled;
65      }
66  
67      public void info(String msg,Object arg0, Object arg1)
68      {
69          if (out == null) return;
70          /* a bit of filtering in debug mode */
71          if (debug && (msg.startsWith("loaded class") || msg.startsWith("loaded interface"))) {
72              return;
73          }
74          logString(_dateCache.now() + " " + name + " " + format(msg,arg0,arg1));
75      }
76  
77      public void debug(String msg,Throwable th)
78      {
79          if (debug)
80          {
81              if (out == null) return;
82              /* a bit of filtering in debug mode */
83              if (debug && (msg.startsWith("loaded class") || msg.startsWith("loaded interface"))) {
84                  return;
85              }
86              logString(_dateCache.now()+" "+msg);
87              logStackTrace(th);
88          }
89      }
90  
91      public void debug(String msg,Object arg0, Object arg1)
92      {
93          if (debug)
94          {
95              if (out == null) return;
96              /* a bit of filtering in debug mode */
97              if (debug && (msg.startsWith("loaded class") || msg.startsWith("loaded interface"))) {
98                  return;
99              }
100             logString(_dateCache.now()+" "+format(msg,arg0,arg1));
101         }
102     }
103 
104     public void warn(String msg,Object arg0, Object arg1)
105     {
106         if (out == null) return;
107         logString(_dateCache.now()+" "+format(msg,arg0,arg1));
108     }
109 
110     public void warn(String msg, Throwable th)
111     {
112         if (out == null) return;
113         logString(_dateCache.now()+" "+msg);
114         logStackTrace(th);
115     }
116 
117     public Logger getLogger(String name) {
118         if ((name==null && this.name==null) ||
119             (name!=null && name.equals(this.name)))
120             return this;
121         return new JettyLogger(name);
122     }
123 
124     /*
125      * private helpers
126      */
127 
128     private synchronized void logString(String msg) {
129         out.println(msg);
130         out.flush();
131     }
132 
133     private synchronized void logStackTrace(Throwable th) {
134         th.printStackTrace(out);
135         out.flush();
136     }
137 
138     private String format(String msg, Object arg0, Object arg1)
139     {
140         int i0=msg.indexOf("{}");
141         int i1=i0<0?-1:msg.indexOf("{}",i0+2);
142 
143         if (arg1!=null && i1>=0)
144             msg=msg.substring(0,i1)+arg1+msg.substring(i1+2);
145         if (arg0!=null && i0>=0)
146             msg=msg.substring(0,i0)+arg0+msg.substring(i0+2);
147         return msg;
148     }
149 
150 }