1 25 package com.mysql.jdbc.log; 26 27 import com.mysql.jdbc.profiler.ProfilerEvent; 28 29 import java.util.logging.Level ; 30 import java.util.logging.Logger ; 31 32 39 public class Jdk14Logger implements Log { 40 private static final Level DEBUG = Level.FINE; 41 42 private static final Level ERROR = Level.SEVERE; 43 44 private static final Level FATAL = Level.SEVERE; 45 46 private static final Level INFO = Level.INFO; 47 48 private static final Level TRACE = Level.FINEST; 49 50 private static final Level WARN = Level.WARNING; 51 52 55 protected Logger jdkLogger = null; 56 57 63 public Jdk14Logger(String name) { 64 this.jdkLogger = Logger.getLogger(name); 65 } 66 67 70 public boolean isDebugEnabled() { 71 return this.jdkLogger.isLoggable(Level.FINE); 72 } 73 74 77 public boolean isErrorEnabled() { 78 return this.jdkLogger.isLoggable(Level.SEVERE); 79 } 80 81 84 public boolean isFatalEnabled() { 85 return this.jdkLogger.isLoggable(Level.SEVERE); 86 } 87 88 91 public boolean isInfoEnabled() { 92 return this.jdkLogger.isLoggable(Level.INFO); 93 } 94 95 98 public boolean isTraceEnabled() { 99 return this.jdkLogger.isLoggable(Level.FINEST); 100 } 101 102 105 public boolean isWarnEnabled() { 106 return this.jdkLogger.isLoggable(Level.WARNING); 107 } 108 109 115 public void logDebug(Object message) { 116 logInternal(DEBUG, message, null); 117 } 118 119 127 public void logDebug(Object message, Throwable exception) { 128 logInternal(DEBUG, message, exception); 129 } 130 131 137 public void logError(Object message) { 138 logInternal(ERROR, message, null); 139 } 140 141 149 public void logError(Object message, Throwable exception) { 150 logInternal(ERROR, message, exception); 151 } 152 153 159 public void logFatal(Object message) { 160 logInternal(FATAL, message, null); 161 } 162 163 171 public void logFatal(Object message, Throwable exception) { 172 logInternal(FATAL, message, exception); 173 } 174 175 181 public void logInfo(Object message) { 182 logInternal(INFO, message, null); 183 } 184 185 193 public void logInfo(Object message, Throwable exception) { 194 logInternal(INFO, message, exception); 195 } 196 197 203 public void logTrace(Object message) { 204 logInternal(TRACE, message, null); 205 } 206 207 215 public void logTrace(Object message, Throwable exception) { 216 logInternal(TRACE, message, exception); 217 } 218 219 225 public void logWarn(Object message) { 226 logInternal(WARN, message, null); 227 } 228 229 237 public void logWarn(Object message, Throwable exception) { 238 logInternal(WARN, message, exception); 239 } 240 241 private static final int findCallerStackDepth(StackTraceElement [] stackTrace) { 242 int numFrames = stackTrace.length; 243 244 for (int i = 0; i < numFrames; i++) { 245 String callerClassName = stackTrace[i].getClassName(); 246 247 if (!callerClassName.startsWith("com.mysql.jdbc") 248 || callerClassName.startsWith("com.mysql.jdbc.compliance")) { 249 return i; 250 } 251 } 252 253 return 0; 254 } 255 256 private void logInternal(Level level, Object msg, Throwable exception) { 257 262 if (this.jdkLogger.isLoggable(level)) { 263 String messageAsString = null; 264 String callerMethodName = "N/A"; 265 String callerClassName = "N/A"; 266 int lineNumber = 0; 267 String fileName = "N/A"; 268 269 if (msg instanceof ProfilerEvent) { 270 messageAsString = LogUtils.expandProfilerEventIfNecessary(msg) 271 .toString(); 272 } else { 273 Throwable locationException = new Throwable (); 274 StackTraceElement [] locations = locationException 275 .getStackTrace(); 276 277 int frameIdx = findCallerStackDepth(locations); 278 279 if (frameIdx != 0) { 280 callerClassName = locations[frameIdx].getClassName(); 281 callerMethodName = locations[frameIdx].getMethodName(); 282 lineNumber = locations[frameIdx].getLineNumber(); 283 fileName = locations[frameIdx].getFileName(); 284 } 285 286 messageAsString = String.valueOf(msg); 287 } 288 289 if (exception == null) { 290 this.jdkLogger.logp(level, callerClassName, callerMethodName, 291 messageAsString); 292 } else { 293 this.jdkLogger.logp(level, callerClassName, callerMethodName, 294 messageAsString, exception); 295 } 296 } 297 } 298 } 299 | Popular Tags |