View Javadoc

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