1 16 17 package org.apache.velocity.tools.generic.log; 18 19 import java.io.StringWriter ; 20 import java.io.PrintWriter ; 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 39 public class LogSystemCommonsLog implements Log 40 { 41 42 private boolean printStackTrace = false; 43 44 45 48 public LogSystemCommonsLog() {} 49 50 51 54 public LogSystemCommonsLog(String name) {} 55 56 57 66 public LogSystemCommonsLog(boolean pst) 67 { 68 this.printStackTrace = pst; 69 } 70 71 72 private void log(int level, Object 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 message, Throwable t) 96 { 97 if (printStackTrace) 98 { 99 StringWriter sw = new StringWriter (); 100 sw.write(String.valueOf(message)); 101 t.printStackTrace(new PrintWriter (sw)); 102 log(level, sw); 103 } 104 else 105 { 106 StringBuffer buffer = new StringBuffer (String.valueOf(message)); 107 buffer.append(" - "); 108 buffer.append(t.getMessage()); 109 log(level, buffer); 110 } 111 } 112 113 114 115 116 120 public void trace(Object message) 121 { 122 log(LogSystem.DEBUG_ID, message); 123 } 124 125 129 public void trace(Object message, Throwable t) 130 { 131 log(LogSystem.DEBUG_ID, message, t); 132 } 133 134 137 public void debug(Object message) 138 { 139 log(LogSystem.DEBUG_ID, message); 140 } 141 142 145 public void debug(Object message, Throwable t) 146 { 147 log(LogSystem.DEBUG_ID, message, t); 148 } 149 150 153 public void info(Object message) 154 { 155 log(LogSystem.INFO_ID, message); 156 } 157 158 161 public void info(Object message, Throwable t) 162 { 163 log(LogSystem.INFO_ID, message, t); 164 } 165 166 169 public void warn(Object message) 170 { 171 log(LogSystem.WARN_ID, message); 172 } 173 174 177 public void warn(Object message, Throwable t) 178 { 179 log(LogSystem.WARN_ID, message, t); 180 } 181 182 185 public void error(Object message) 186 { 187 log(LogSystem.ERROR_ID, message); 188 } 189 190 193 public void error(Object message, Throwable t) 194 { 195 log(LogSystem.ERROR_ID, message, t); 196 } 197 198 202 public void fatal(Object message) 203 { 204 log(LogSystem.ERROR_ID, message); 205 } 206 207 211 public void fatal(Object message, Throwable t) 212 { 213 log(LogSystem.ERROR_ID, message, t); 214 } 215 216 222 public boolean isTraceEnabled() { return true; } 223 224 230 public boolean isDebugEnabled() { return true; } 231 232 238 public boolean isInfoEnabled() { return true; } 239 240 246 public boolean isWarnEnabled() { return true; } 247 248 254 public boolean isErrorEnabled() { return true; } 255 256 262 public boolean isFatalEnabled() { return true; } 263 264 } 265 | Popular Tags |