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 724825 2008-12-09 18:56:06Z 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) 80 { 81 getLogChute().log(level, String.valueOf(message)); 82 } 83 84 protected void log(int level, Object message, Throwable t) 85 { 86 getLogChute().log(level, String.valueOf(message), t); 87 } 88 89 /** 90 * Returns true if trace level messages will be printed by the LogChute. 91 * @return If trace level messages will be printed by the LogChute. 92 */ 93 public boolean isTraceEnabled() 94 { 95 return getLogChute().isLevelEnabled(LogChute.TRACE_ID); 96 } 97 98 /** 99 * Log a trace message. 100 * @param message 101 */ 102 public void trace(Object message) 103 { 104 log(LogChute.TRACE_ID, message); 105 } 106 107 /** 108 * Log a trace message and accompanying Throwable. 109 * @param message 110 * @param t 111 */ 112 public void trace(Object message, Throwable t) 113 { 114 log(LogChute.TRACE_ID, message, t); 115 } 116 117 /** 118 * Returns true if debug level messages will be printed by the LogChute. 119 * @return True if debug level messages will be printed by the LogChute. 120 */ 121 public boolean isDebugEnabled() 122 { 123 return getLogChute().isLevelEnabled(LogChute.DEBUG_ID); 124 } 125 126 /** 127 * Log a debug message. 128 * @param message 129 */ 130 public void debug(Object message) 131 { 132 log(LogChute.DEBUG_ID, message); 133 } 134 135 /** 136 * Log a debug message and accompanying Throwable. 137 * @param message 138 * @param t 139 */ 140 public void debug(Object message, Throwable t) 141 { 142 log(LogChute.DEBUG_ID, message, t); 143 } 144 145 /** 146 * Returns true if info level messages will be printed by the LogChute. 147 * @return True if info level messages will be printed by the LogChute. 148 */ 149 public boolean isInfoEnabled() 150 { 151 return getLogChute().isLevelEnabled(LogChute.INFO_ID); 152 } 153 154 /** 155 * Log an info message. 156 * @param message 157 */ 158 public void info(Object message) 159 { 160 log(LogChute.INFO_ID, message); 161 } 162 163 /** 164 * Log an info message and accompanying Throwable. 165 * @param message 166 * @param t 167 */ 168 public void info(Object message, Throwable t) 169 { 170 log(LogChute.INFO_ID, message, t); 171 } 172 173 /** 174 * Returns true if warn level messages will be printed by the LogChute. 175 * @return True if warn level messages will be printed by the LogChute. 176 */ 177 public boolean isWarnEnabled() 178 { 179 return getLogChute().isLevelEnabled(LogChute.WARN_ID); 180 } 181 182 /** 183 * Log a warning message. 184 * @param message 185 */ 186 public void warn(Object message) 187 { 188 log(LogChute.WARN_ID, message); 189 } 190 191 /** 192 * Log a warning message and accompanying Throwable. 193 * @param message 194 * @param t 195 */ 196 public void warn(Object message, Throwable t) 197 { 198 log(LogChute.WARN_ID, message, t); 199 } 200 201 /** 202 * Returns true if error level messages will be printed by the LogChute. 203 * @return True if error level messages will be printed by the LogChute. 204 */ 205 public boolean isErrorEnabled() 206 { 207 return getLogChute().isLevelEnabled(LogChute.ERROR_ID); 208 } 209 210 /** 211 * Log an error message. 212 * @param message 213 */ 214 public void error(Object message) 215 { 216 log(LogChute.ERROR_ID, message); 217 } 218 219 /** 220 * Log an error message and accompanying Throwable. 221 * @param message 222 * @param t 223 */ 224 public void error(Object message, Throwable t) 225 { 226 log(LogChute.ERROR_ID, message, t); 227 } 228 229 /** 230 * Creates a string that formats the template filename with line number 231 * and column of the given Directive. We use this routine to provide a cosistent format for displaying 232 * file errors. 233 */ 234 public static final String formatFileString(Directive directive) 235 { 236 return formatFileString(directive.getTemplateName(), directive.getLine(), directive.getColumn()); 237 } 238 239 /** 240 * Creates a string that formats the template filename with line number 241 * and column of the given Node. We use this routine to provide a cosistent format for displaying 242 * file errors. 243 */ 244 public static final String formatFileString(Node node) 245 { 246 return formatFileString(node.getTemplateName(), node.getLine(), node.getColumn()); 247 } 248 249 /** 250 * Simply creates a string that formats the template filename with line number 251 * and column. We use this routine to provide a cosistent format for displaying 252 * file errors. 253 */ 254 public static final String formatFileString(Info info) 255 { 256 return formatFileString(info.getTemplateName(), info.getLine(), info.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 * @param template File name of template, can be null 264 * @param linenum Line number within the file 265 * @param colnum Column number withing the file at linenum 266 */ 267 public static final String formatFileString(String template, int linenum, int colnum) 268 { 269 if (template == null || template.equals("")) 270 { 271 template = "<unknown template>"; 272 } 273 return template + "[line " + linenum + ", column " + colnum + "]"; 274 } 275 }