KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > velocity > tools > generic > log > LogSystemCommonsLog


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.generic.log;
18
19 import java.io.StringWriter JavaDoc;
20 import java.io.PrintWriter JavaDoc;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.velocity.app.Velocity;
24 import org.apache.velocity.runtime.log.LogSystem;
25
26 /**
27  * Redirects commons-logging messages to Velocity's LogSystem.
28  *
29  * <p>To use, specify this class in your commons-logging.properties:
30  * <code>
31  * org.apache.commons.logging.Log=org.apache.velocity.tools.generic.log.LogSystemCommonsLog
32  * </code>
33  * </p>
34  *
35  * @author <a HREF="mailto:nathan@esha.com">Nathan Bubna</a>
36  * @since VelocityTools 1.1
37  * @version $Id: LogSystemCommonsLog.java,v 1.3 2004/02/18 20:10:38 nbubna Exp $
38  */

39 public class LogSystemCommonsLog implements Log
40 {
41
42     private boolean printStackTrace = false;
43
44
45     /**
46      * Default constructor
47      */

48     public LogSystemCommonsLog() {}
49
50
51     /**
52      * Meaningless constructor to make commons-logging happy.
53      */

54     public LogSystemCommonsLog(String JavaDoc name) {}
55
56
57     /**
58      * Lets you set whether or not this instance should print the
59      * full stack trace of exceptions and errors passed to it.
60      *
61      * <p>It should be possible to create a LogFactory implementation
62      * that takes advantage of this constructor.</p>
63      *
64      * @param pst if true, stack traces will be printed
65      */

66     public LogSystemCommonsLog(boolean pst)
67     {
68         this.printStackTrace = pst;
69     }
70
71     
72     private void log(int level, Object JavaDoc message)
73     {
74         switch (level)
75         {
76             case LogSystem.WARN_ID:
77                 Velocity.warn(message);
78                 break;
79             case LogSystem.INFO_ID:
80                 Velocity.info(message);
81                 break;
82             case LogSystem.DEBUG_ID:
83                 Velocity.debug(message);
84                 break;
85             case LogSystem.ERROR_ID:
86                 Velocity.error(message);
87                 break;
88             default:
89                 Velocity.debug(message);
90                 break;
91         }
92     }
93
94     
95     private void log(int level, Object JavaDoc message, Throwable JavaDoc t)
96     {
97         if (printStackTrace)
98         {
99             StringWriter JavaDoc sw = new StringWriter JavaDoc();
100             sw.write(String.valueOf(message));
101             t.printStackTrace(new PrintWriter JavaDoc(sw));
102             log(level, sw);
103         }
104         else
105         {
106             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(String.valueOf(message));
107             buffer.append(" - ");
108             buffer.append(t.getMessage());
109             log(level, buffer);
110         }
111     }
112
113
114     /*************** Commons Log Interface ****************/
115
116     /**
117      * Passes messages to Velocity's LogSystem at "DEBUG" level.
118      * (it's the lowest available. sorry.)
119      */

120     public void trace(Object JavaDoc message)
121     {
122         log(LogSystem.DEBUG_ID, message);
123     }
124
125     /**
126      * Passes messages to Velocity's LogSystem at "DEBUG" level.
127      * (it's the lowest available. sorry.)
128      */

129     public void trace(Object JavaDoc message, Throwable JavaDoc t)
130     {
131         log(LogSystem.DEBUG_ID, message, t);
132     }
133
134     /**
135      * Passes messages to Velocity's LogSystem at "DEBUG" level.
136      */

137     public void debug(Object JavaDoc message)
138     {
139         log(LogSystem.DEBUG_ID, message);
140     }
141
142     /**
143      * Passes messages to Velocity's LogSystem at "DEBUG" level.
144      */

145     public void debug(Object JavaDoc message, Throwable JavaDoc t)
146     {
147         log(LogSystem.DEBUG_ID, message, t);
148     }
149
150     /**
151      * Passes messages to Velocity's LogSystem at "INFO" level.
152      */

153     public void info(Object JavaDoc message)
154     {
155         log(LogSystem.INFO_ID, message);
156     }
157
158     /**
159      * Passes messages to Velocity's LogSystem at "INFO" level.
160      */

161     public void info(Object JavaDoc message, Throwable JavaDoc t)
162     {
163         log(LogSystem.INFO_ID, message, t);
164     }
165
166     /**
167      * Passes messages to Velocity's LogSystem at "WARN" level.
168      */

169     public void warn(Object JavaDoc message)
170     {
171         log(LogSystem.WARN_ID, message);
172     }
173
174     /**
175      * Passes messages to Velocity's LogSystem at "WARN" level.
176      */

177     public void warn(Object JavaDoc message, Throwable JavaDoc t)
178     {
179         log(LogSystem.WARN_ID, message, t);
180     }
181
182     /**
183      * Passes messages to Velocity's LogSystem at "ERROR" level.
184      */

185     public void error(Object JavaDoc message)
186     {
187         log(LogSystem.ERROR_ID, message);
188     }
189
190     /**
191      * Passes messages to Velocity's LogSystem at "ERROR" level.
192      */

193     public void error(Object JavaDoc message, Throwable JavaDoc t)
194     {
195         log(LogSystem.ERROR_ID, message, t);
196     }
197
198     /**
199      * Passes messages to Velocity's LogSystem at "ERROR" level.
200      * (it's the highest available. sorry.)
201      */

202     public void fatal(Object JavaDoc message)
203     {
204         log(LogSystem.ERROR_ID, message);
205     }
206
207     /**
208      * Passes messages to Velocity's LogSystem at "ERROR" level.
209      * (it's the highest available. sorry.)
210      */

211     public void fatal(Object JavaDoc message, Throwable JavaDoc t)
212     {
213         log(LogSystem.ERROR_ID, message, t);
214     }
215
216     /**
217      * Always returns true since Velocity's LogSystem
218      * doesn't provide this information.
219      *
220      * @return true
221      */

222     public boolean isTraceEnabled() { return true; }
223
224     /**
225      * Always returns true since Velocity's LogSystem
226      * doesn't provide this information.
227      *
228      * @return true
229      */

230     public boolean isDebugEnabled() { return true; }
231
232     /**
233      * Always returns true since Velocity's LogSystem
234      * doesn't provide this information.
235      *
236      * @return true
237      */

238     public boolean isInfoEnabled() { return true; }
239
240     /**
241      * Always returns true since Velocity's LogSystem
242      * doesn't provide this information.
243      *
244      * @return true
245      */

246     public boolean isWarnEnabled() { return true; }
247
248     /**
249      * Always returns true since Velocity's LogSystem
250      * doesn't provide this information.
251      *
252      * @return true
253      */

254     public boolean isErrorEnabled() { return true; }
255
256     /**
257      * Always returns true since Velocity's LogSystem
258      * doesn't provide this information.
259      *
260      * @return true
261      */

262     public boolean isFatalEnabled() { return true; }
263
264 }
265
Popular Tags