| 1 | /* |
| 2 | Copyright (C) 2002-2004 MySQL AB |
| 3 | |
| 4 | This program is free software; you can redistribute it and/or modify |
| 5 | it under the terms of version 2 of the GNU General Public License as |
| 6 | published by the Free Software Foundation. |
| 7 | |
| 8 | There are special exceptions to the terms and conditions of the GPL |
| 9 | as it is applied to this software. View the full text of the |
| 10 | exception in file EXCEPTIONS-CONNECTOR-J in the directory of this |
| 11 | software distribution. |
| 12 | |
| 13 | This program is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with this program; if not, write to the Free Software |
| 20 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | |
| 22 | |
| 23 | |
| 24 | */ |
| 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 | /** |
| 33 | * Logging functionality for JDK1.4 |
| 34 | * |
| 35 | * @author Mark Matthews |
| 36 | * |
| 37 | * @version $Id: Jdk14Logger.java 3726 2005-05-19 15:52:24Z mmatthews $ |
| 38 | */ |
| 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 | /** |
| 53 | * The underlying logger from JDK-1.4 |
| 54 | */ |
| 55 | protected Logger jdkLogger = null; |
| 56 | |
| 57 | /** |
| 58 | * Creates a new Jdk14Logger object. |
| 59 | * |
| 60 | * @param name |
| 61 | * DOCUMENT ME! |
| 62 | */ |
| 63 | public Jdk14Logger(String name) { |
| 64 | this.jdkLogger = Logger.getLogger(name); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * @see com.mysql.jdbc.log.Log#isDebugEnabled() |
| 69 | */ |
| 70 | public boolean isDebugEnabled() { |
| 71 | return this.jdkLogger.isLoggable(Level.FINE); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * @see com.mysql.jdbc.log.Log#isErrorEnabled() |
| 76 | */ |
| 77 | public boolean isErrorEnabled() { |
| 78 | return this.jdkLogger.isLoggable(Level.SEVERE); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * @see com.mysql.jdbc.log.Log#isFatalEnabled() |
| 83 | */ |
| 84 | public boolean isFatalEnabled() { |
| 85 | return this.jdkLogger.isLoggable(Level.SEVERE); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * @see com.mysql.jdbc.log.Log#isInfoEnabled() |
| 90 | */ |
| 91 | public boolean isInfoEnabled() { |
| 92 | return this.jdkLogger.isLoggable(Level.INFO); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * @see com.mysql.jdbc.log.Log#isTraceEnabled() |
| 97 | */ |
| 98 | public boolean isTraceEnabled() { |
| 99 | return this.jdkLogger.isLoggable(Level.FINEST); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * @see com.mysql.jdbc.log.Log#isWarnEnabled() |
| 104 | */ |
| 105 | public boolean isWarnEnabled() { |
| 106 | return this.jdkLogger.isLoggable(Level.WARNING); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Logs the given message instance using the 'debug' level |
| 111 | * |
| 112 | * @param message |
| 113 | * the message to log |
| 114 | */ |
| 115 | public void logDebug(Object message) { |
| 116 | logInternal(DEBUG, message, null); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Logs the given message and Throwable at the 'debug' level. |
| 121 | * |
| 122 | * @param message |
| 123 | * the message to log |
| 124 | * @param exception |
| 125 | * the throwable to log (may be null) |
| 126 | */ |
| 127 | public void logDebug(Object message, Throwable exception) { |
| 128 | logInternal(DEBUG, message, exception); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Logs the given message instance using the 'error' level |
| 133 | * |
| 134 | * @param message |
| 135 | * the message to log |
| 136 | */ |
| 137 | public void logError(Object message) { |
| 138 | logInternal(ERROR, message, null); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Logs the given message and Throwable at the 'error' level. |
| 143 | * |
| 144 | * @param message |
| 145 | * the message to log |
| 146 | * @param exception |
| 147 | * the throwable to log (may be null) |
| 148 | */ |
| 149 | public void logError(Object message, Throwable exception) { |
| 150 | logInternal(ERROR, message, exception); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Logs the given message instance using the 'fatal' level |
| 155 | * |
| 156 | * @param message |
| 157 | * the message to log |
| 158 | */ |
| 159 | public void logFatal(Object message) { |
| 160 | logInternal(FATAL, message, null); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Logs the given message and Throwable at the 'fatal' level. |
| 165 | * |
| 166 | * @param message |
| 167 | * the message to log |
| 168 | * @param exception |
| 169 | * the throwable to log (may be null) |
| 170 | */ |
| 171 | public void logFatal(Object message, Throwable exception) { |
| 172 | logInternal(FATAL, message, exception); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Logs the given message instance using the 'info' level |
| 177 | * |
| 178 | * @param message |
| 179 | * the message to log |
| 180 | */ |
| 181 | public void logInfo(Object message) { |
| 182 | logInternal(INFO, message, null); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Logs the given message and Throwable at the 'info' level. |
| 187 | * |
| 188 | * @param message |
| 189 | * the message to log |
| 190 | * @param exception |
| 191 | * the throwable to log (may be null) |
| 192 | */ |
| 193 | public void logInfo(Object message, Throwable exception) { |
| 194 | logInternal(INFO, message, exception); |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Logs the given message instance using the 'trace' level |
| 199 | * |
| 200 | * @param message |
| 201 | * the message to log |
| 202 | */ |
| 203 | public void logTrace(Object message) { |
| 204 | logInternal(TRACE, message, null); |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Logs the given message and Throwable at the 'trace' level. |
| 209 | * |
| 210 | * @param message |
| 211 | * the message to log |
| 212 | * @param exception |
| 213 | * the throwable to log (may be null) |
| 214 | */ |
| 215 | public void logTrace(Object message, Throwable exception) { |
| 216 | logInternal(TRACE, message, exception); |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Logs the given message instance using the 'warn' level |
| 221 | * |
| 222 | * @param message |
| 223 | * the message to log |
| 224 | */ |
| 225 | public void logWarn(Object message) { |
| 226 | logInternal(WARN, message, null); |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Logs the given message and Throwable at the 'warn' level. |
| 231 | * |
| 232 | * @param message |
| 233 | * the message to log |
| 234 | * @param exception |
| 235 | * the throwable to log (may be null) |
| 236 | */ |
| 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 | // |
| 258 | // only go through this exercise if the message will actually |
| 259 | // be logged. |
| 260 | // |
| 261 | |
| 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 | } |