1 25 package com.mysql.jdbc.log; 26 27 import com.mysql.jdbc.Util; 28 import com.mysql.jdbc.profiler.ProfilerEvent; 29 30 import java.util.Date ; 31 32 40 public class StandardLogger implements Log { 41 private static final int FATAL = 0; 42 43 private static final int ERROR = 1; 44 45 private static final int WARN = 2; 46 47 private static final int INFO = 3; 48 49 private static final int DEBUG = 4; 50 51 private static final int TRACE = 5; 52 53 public static StringBuffer bufferedLog = null; 54 55 private boolean logLocationInfo = true; 56 57 63 public StandardLogger(String name) { 64 this(name, false); 65 } 66 67 public StandardLogger(String name, boolean logLocationInfo) { 68 this.logLocationInfo = logLocationInfo; 69 } 70 71 public static void saveLogsToBuffer() { 72 if (bufferedLog == null) { 73 bufferedLog = new StringBuffer (); 74 } 75 } 76 77 80 public boolean isDebugEnabled() { 81 return true; 82 } 83 84 87 public boolean isErrorEnabled() { 88 return true; 89 } 90 91 94 public boolean isFatalEnabled() { 95 return true; 96 } 97 98 101 public boolean isInfoEnabled() { 102 return true; 103 } 104 105 108 public boolean isTraceEnabled() { 109 return true; 110 } 111 112 115 public boolean isWarnEnabled() { 116 return true; 117 } 118 119 125 public void logDebug(Object message) { 126 logInternal(DEBUG, message, null); 127 } 128 129 137 public void logDebug(Object message, Throwable exception) { 138 logInternal(DEBUG, message, exception); 139 } 140 141 147 public void logError(Object message) { 148 logInternal(ERROR, message, null); 149 } 150 151 159 public void logError(Object message, Throwable exception) { 160 logInternal(ERROR, message, exception); 161 } 162 163 169 public void logFatal(Object message) { 170 logInternal(FATAL, message, null); 171 } 172 173 181 public void logFatal(Object message, Throwable exception) { 182 logInternal(FATAL, message, exception); 183 } 184 185 191 public void logInfo(Object message) { 192 logInternal(INFO, message, null); 193 } 194 195 203 public void logInfo(Object message, Throwable exception) { 204 logInternal(INFO, message, exception); 205 } 206 207 213 public void logTrace(Object message) { 214 logInternal(TRACE, message, null); 215 } 216 217 225 public void logTrace(Object message, Throwable exception) { 226 logInternal(TRACE, message, exception); 227 } 228 229 235 public void logWarn(Object message) { 236 logInternal(WARN, message, null); 237 } 238 239 247 public void logWarn(Object message, Throwable exception) { 248 logInternal(WARN, message, exception); 249 } 250 251 private void logInternal(int level, Object msg, Throwable exception) { 252 StringBuffer msgBuf = new StringBuffer (); 253 msgBuf.append(new Date ().toString()); 254 msgBuf.append(" "); 255 256 switch (level) { 257 case FATAL: 258 msgBuf.append("FATAL: "); 259 260 break; 261 262 case ERROR: 263 msgBuf.append("ERROR: "); 264 265 break; 266 267 case WARN: 268 msgBuf.append("WARN: "); 269 270 break; 271 272 case INFO: 273 msgBuf.append("INFO: "); 274 275 break; 276 277 case DEBUG: 278 msgBuf.append("DEBUG: "); 279 280 break; 281 282 case TRACE: 283 msgBuf.append("TRACE: "); 284 285 break; 286 } 287 288 if (msg instanceof ProfilerEvent) { 289 msgBuf.append(LogUtils.expandProfilerEventIfNecessary(msg)); 290 291 } else { 302 } 303 304 if (exception != null) { 305 msgBuf.append("\n"); 306 msgBuf.append("\n"); 307 msgBuf.append("EXCEPTION STACK TRACE:"); 308 msgBuf.append("\n"); 309 msgBuf.append("\n"); 310 msgBuf.append(Util.stackTraceToString(exception)); 311 } 312 313 String messageAsString = msgBuf.toString(); 314 315 System.err.println(messageAsString); 316 317 if (bufferedLog != null) { 318 bufferedLog.append(messageAsString); 319 } 320 } 321 } 322 | Popular Tags |