1 package org.apache.velocity.runtime.log;
2
3 import org.apache.velocity.runtime.parser.node.Node;
4 import org.apache.velocity.util.introspection.Info;
5
6 /*
7 * Licensed to the Apache Software Foundation (ASF) under one
8 * or more contributor license agreements. See the NOTICE file
9 * distributed with this work for additional information
10 * regarding copyright ownership. The ASF licenses this file
11 * to you under the Apache License, Version 2.0 (the
12 * "License"); you may not use this file except in compliance
13 * with the License. You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing,
18 * software distributed under the License is distributed on an
19 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20 * KIND, either express or implied. See the License for the
21 * specific language governing permissions and limitations
22 * under the License.
23 */
24
25 /**
26 * Convenient wrapper for LogChute functions. This implements
27 * the RuntimeLogger methods (and then some). It is hoped that
28 * use of this will fully replace use of the RuntimeLogger.
29 *
30 * @author <a href="mailto:nbubna@apache.org">Nathan Bubna</a>
31 * @version $Id: Log.java 720228 2008-11-24 16:58:33Z nbubna $
32 * @since 1.5
33 */
34 public class Log
35 {
36
37 private LogChute chute;
38
39 /**
40 * Creates a new Log that wraps a HoldingLogChute.
41 */
42 public Log()
43 {
44 setLogChute(new HoldingLogChute());
45 }
46
47 /**
48 * Creates a new Log that wraps the specified LogChute.
49 * @param chute
50 */
51 public Log(final LogChute chute)
52 {
53 setLogChute(chute);
54 }
55
56 /**
57 * Updates the LogChute wrapped by this Log instance.
58 * @param chute The new value for the log chute.
59 */
60 protected void setLogChute(final LogChute chute)
61 {
62 if (chute == null)
63 {
64 throw new NullPointerException("The LogChute cannot be set to null!");
65 }
66 this.chute = chute;
67 }
68
69 /**
70 * Returns the LogChute wrapped by this Log instance.
71 * @return The LogChute wrapped by this Log instance.
72 */
73 protected LogChute getLogChute()
74 {
75 return this.chute;
76 }
77
78 protected void log(int level, Object message)
79 {
80 getLogChute().log(level, String.valueOf(message));
81 }
82
83 protected void log(int level, Object message, Throwable t)
84 {
85 getLogChute().log(level, String.valueOf(message), t);
86 }
87
88 /**
89 * Returns true if trace level messages will be printed by the LogChute.
90 * @return If trace level messages will be printed by the LogChute.
91 */
92 public boolean isTraceEnabled()
93 {
94 return getLogChute().isLevelEnabled(LogChute.TRACE_ID);
95 }
96
97 /**
98 * Log a trace message.
99 * @param message
100 */
101 public void trace(Object message)
102 {
103 log(LogChute.TRACE_ID, message);
104 }
105
106 /**
107 * Log a trace message and accompanying Throwable.
108 * @param message
109 * @param t
110 */
111 public void trace(Object message, Throwable t)
112 {
113 log(LogChute.TRACE_ID, message, t);
114 }
115
116 /**
117 * Returns true if debug level messages will be printed by the LogChute.
118 * @return True if debug level messages will be printed by the LogChute.
119 */
120 public boolean isDebugEnabled()
121 {
122 return getLogChute().isLevelEnabled(LogChute.DEBUG_ID);
123 }
124
125 /**
126 * Log a debug message.
127 * @param message
128 */
129 public void debug(Object message)
130 {
131 log(LogChute.DEBUG_ID, message);
132 }
133
134 /**
135 * Log a debug message and accompanying Throwable.
136 * @param message
137 * @param t
138 */
139 public void debug(Object message, Throwable t)
140 {
141 log(LogChute.DEBUG_ID, message, t);
142 }
143
144 /**
145 * Returns true if info level messages will be printed by the LogChute.
146 * @return True if info level messages will be printed by the LogChute.
147 */
148 public boolean isInfoEnabled()
149 {
150 return getLogChute().isLevelEnabled(LogChute.INFO_ID);
151 }
152
153 /**
154 * Log an info message.
155 * @param message
156 */
157 public void info(Object message)
158 {
159 log(LogChute.INFO_ID, message);
160 }
161
162 /**
163 * Log an info message and accompanying Throwable.
164 * @param message
165 * @param t
166 */
167 public void info(Object message, Throwable t)
168 {
169 log(LogChute.INFO_ID, message, t);
170 }
171
172 /**
173 * Returns true if warn level messages will be printed by the LogChute.
174 * @return True if warn level messages will be printed by the LogChute.
175 */
176 public boolean isWarnEnabled()
177 {
178 return getLogChute().isLevelEnabled(LogChute.WARN_ID);
179 }
180
181 /**
182 * Log a warning message.
183 * @param message
184 */
185 public void warn(Object message)
186 {
187 log(LogChute.WARN_ID, message);
188 }
189
190 /**
191 * Log a warning message and accompanying Throwable.
192 * @param message
193 * @param t
194 */
195 public void warn(Object message, Throwable t)
196 {
197 log(LogChute.WARN_ID, message, t);
198 }
199
200 /**
201 * Returns true if error level messages will be printed by the LogChute.
202 * @return True if error level messages will be printed by the LogChute.
203 */
204 public boolean isErrorEnabled()
205 {
206 return getLogChute().isLevelEnabled(LogChute.ERROR_ID);
207 }
208
209 /**
210 * Log an error message.
211 * @param message
212 */
213 public void error(Object message)
214 {
215 log(LogChute.ERROR_ID, message);
216 }
217
218 /**
219 * Log an error message and accompanying Throwable.
220 * @param message
221 * @param t
222 */
223 public void error(Object message, Throwable t)
224 {
225 log(LogChute.ERROR_ID, message, t);
226 }
227
228 /**
229 * Creates a string that formats the template filename with line number
230 * and column of the given Node. We use this routine to provide a cosistent format for displaying
231 * file errors.
232 */
233 public static final String formatFileString(Node node)
234 {
235 return formatFileString(node.getTemplateName(), node.getLine(), node.getColumn());
236 }
237
238 /**
239 * Simply creates a string that formats the template filename with line number
240 * and column. We use this routine to provide a cosistent format for displaying
241 * file errors.
242 */
243 public static final String formatFileString(Info info)
244 {
245 return formatFileString(info.getTemplateName(), info.getLine(), info.getColumn());
246 }
247
248 /**
249 * Simply creates a string that formats the template filename with line number
250 * and column. We use this routine to provide a cosistent format for displaying
251 * file errors.
252 * @param template File name of template, can be null
253 * @param linenum Line number within the file
254 * @param colnum Column number withing the file at linenum
255 */
256 public static final String formatFileString(String template, int linenum, int colnum)
257 {
258 if (template == null || template.equals(""))
259 {
260 template = "<unknown template>";
261 }
262 return template + "[line " + linenum + ", column " + colnum + "]";
263 }
264 }