[766] | 1 | /* |
---|
| 2 | Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved. |
---|
| 3 | |
---|
| 4 | |
---|
| 5 | The MySQL Connector/J is licensed under the terms of the GPLv2 |
---|
| 6 | <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most MySQL Connectors. |
---|
| 7 | There are special exceptions to the terms and conditions of the GPLv2 as it is applied to |
---|
| 8 | this software, see the FLOSS License Exception |
---|
| 9 | <http://www.mysql.com/about/legal/licensing/foss-exception.html>. |
---|
| 10 | |
---|
| 11 | This program is free software; you can redistribute it and/or modify it under the terms |
---|
| 12 | of the GNU General Public License as published by the Free Software Foundation; version 2 |
---|
| 13 | of the License. |
---|
| 14 | |
---|
| 15 | This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
---|
| 16 | without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
---|
| 17 | See the GNU General Public License for more details. |
---|
| 18 | |
---|
| 19 | You should have received a copy of the GNU General Public License along with this |
---|
| 20 | program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth |
---|
| 21 | Floor, Boston, MA 02110-1301 USA |
---|
| 22 | |
---|
| 23 | |
---|
| 24 | |
---|
| 25 | */ |
---|
| 26 | package com.mysql.jdbc.log; |
---|
| 27 | |
---|
| 28 | import java.util.Date; |
---|
| 29 | |
---|
| 30 | import com.mysql.jdbc.Util; |
---|
| 31 | import com.mysql.jdbc.profiler.ProfilerEvent; |
---|
| 32 | |
---|
| 33 | /** |
---|
| 34 | * Provides logging facilities for those platforms that don't have built-in |
---|
| 35 | * facilities. Simply logs messages to STDERR. |
---|
| 36 | * |
---|
| 37 | * @author Mark Matthews |
---|
| 38 | * |
---|
| 39 | * @version $Id$ |
---|
| 40 | */ |
---|
| 41 | public class StandardLogger implements Log { |
---|
| 42 | private static final int FATAL = 0; |
---|
| 43 | |
---|
| 44 | private static final int ERROR = 1; |
---|
| 45 | |
---|
| 46 | private static final int WARN = 2; |
---|
| 47 | |
---|
| 48 | private static final int INFO = 3; |
---|
| 49 | |
---|
| 50 | private static final int DEBUG = 4; |
---|
| 51 | |
---|
| 52 | private static final int TRACE = 5; |
---|
| 53 | |
---|
| 54 | public static StringBuffer bufferedLog = null; |
---|
| 55 | |
---|
| 56 | private boolean logLocationInfo = true; |
---|
| 57 | |
---|
| 58 | /** |
---|
| 59 | * Creates a new StandardLogger object. |
---|
| 60 | * |
---|
| 61 | * @param name |
---|
| 62 | * the name of the configuration to use -- ignored |
---|
| 63 | */ |
---|
| 64 | public StandardLogger(String name) { |
---|
| 65 | this(name, false); |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | /** |
---|
| 69 | * |
---|
| 70 | * @param name |
---|
| 71 | * @param logLocationInfo |
---|
| 72 | */ |
---|
| 73 | public StandardLogger(String name, boolean logLocationInfo) { |
---|
| 74 | this.logLocationInfo = logLocationInfo; |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | public static void saveLogsToBuffer() { |
---|
| 78 | if (bufferedLog == null) { |
---|
| 79 | bufferedLog = new StringBuffer(); |
---|
| 80 | } |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | /** |
---|
| 84 | * @see com.mysql.jdbc.log.Log#isDebugEnabled() |
---|
| 85 | */ |
---|
| 86 | public boolean isDebugEnabled() { |
---|
| 87 | return true; |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | /** |
---|
| 91 | * @see com.mysql.jdbc.log.Log#isErrorEnabled() |
---|
| 92 | */ |
---|
| 93 | public boolean isErrorEnabled() { |
---|
| 94 | return true; |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | /** |
---|
| 98 | * @see com.mysql.jdbc.log.Log#isFatalEnabled() |
---|
| 99 | */ |
---|
| 100 | public boolean isFatalEnabled() { |
---|
| 101 | return true; |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | /** |
---|
| 105 | * @see com.mysql.jdbc.log.Log#isInfoEnabled() |
---|
| 106 | */ |
---|
| 107 | public boolean isInfoEnabled() { |
---|
| 108 | return true; |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | /** |
---|
| 112 | * @see com.mysql.jdbc.log.Log#isTraceEnabled() |
---|
| 113 | */ |
---|
| 114 | public boolean isTraceEnabled() { |
---|
| 115 | return true; |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | /** |
---|
| 119 | * @see com.mysql.jdbc.log.Log#isWarnEnabled() |
---|
| 120 | */ |
---|
| 121 | public boolean isWarnEnabled() { |
---|
| 122 | return true; |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | /** |
---|
| 126 | * Logs the given message instance using the 'debug' level |
---|
| 127 | * |
---|
| 128 | * @param message |
---|
| 129 | * the message to log |
---|
| 130 | */ |
---|
| 131 | public void logDebug(Object message) { |
---|
| 132 | logInternal(DEBUG, message, null); |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | /** |
---|
| 136 | * Logs the given message and Throwable at the 'debug' level. |
---|
| 137 | * |
---|
| 138 | * @param message |
---|
| 139 | * the message to log |
---|
| 140 | * @param exception |
---|
| 141 | * the throwable to log (may be null) |
---|
| 142 | */ |
---|
| 143 | public void logDebug(Object message, Throwable exception) { |
---|
| 144 | logInternal(DEBUG, message, exception); |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | /** |
---|
| 148 | * Logs the given message instance using the 'error' level |
---|
| 149 | * |
---|
| 150 | * @param message |
---|
| 151 | * the message to log |
---|
| 152 | */ |
---|
| 153 | public void logError(Object message) { |
---|
| 154 | logInternal(ERROR, message, null); |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | /** |
---|
| 158 | * Logs the given message and Throwable at the 'error' level. |
---|
| 159 | * |
---|
| 160 | * @param message |
---|
| 161 | * the message to log |
---|
| 162 | * @param exception |
---|
| 163 | * the throwable to log (may be null) |
---|
| 164 | */ |
---|
| 165 | public void logError(Object message, Throwable exception) { |
---|
| 166 | logInternal(ERROR, message, exception); |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | /** |
---|
| 170 | * Logs the given message instance using the 'fatal' level |
---|
| 171 | * |
---|
| 172 | * @param message |
---|
| 173 | * the message to log |
---|
| 174 | */ |
---|
| 175 | public void logFatal(Object message) { |
---|
| 176 | logInternal(FATAL, message, null); |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | /** |
---|
| 180 | * Logs the given message and Throwable at the 'fatal' level. |
---|
| 181 | * |
---|
| 182 | * @param message |
---|
| 183 | * the message to log |
---|
| 184 | * @param exception |
---|
| 185 | * the throwable to log (may be null) |
---|
| 186 | */ |
---|
| 187 | public void logFatal(Object message, Throwable exception) { |
---|
| 188 | logInternal(FATAL, message, exception); |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | /** |
---|
| 192 | * Logs the given message instance using the 'info' level |
---|
| 193 | * |
---|
| 194 | * @param message |
---|
| 195 | * the message to log |
---|
| 196 | */ |
---|
| 197 | public void logInfo(Object message) { |
---|
| 198 | logInternal(INFO, message, null); |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | /** |
---|
| 202 | * Logs the given message and Throwable at the 'info' level. |
---|
| 203 | * |
---|
| 204 | * @param message |
---|
| 205 | * the message to log |
---|
| 206 | * @param exception |
---|
| 207 | * the throwable to log (may be null) |
---|
| 208 | */ |
---|
| 209 | public void logInfo(Object message, Throwable exception) { |
---|
| 210 | logInternal(INFO, message, exception); |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | /** |
---|
| 214 | * Logs the given message instance using the 'trace' level |
---|
| 215 | * |
---|
| 216 | * @param message |
---|
| 217 | * the message to log |
---|
| 218 | */ |
---|
| 219 | public void logTrace(Object message) { |
---|
| 220 | logInternal(TRACE, message, null); |
---|
| 221 | } |
---|
| 222 | |
---|
| 223 | /** |
---|
| 224 | * Logs the given message and Throwable at the 'trace' level. |
---|
| 225 | * |
---|
| 226 | * @param message |
---|
| 227 | * the message to log |
---|
| 228 | * @param exception |
---|
| 229 | * the throwable to log (may be null) |
---|
| 230 | */ |
---|
| 231 | public void logTrace(Object message, Throwable exception) { |
---|
| 232 | logInternal(TRACE, message, exception); |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | /** |
---|
| 236 | * Logs the given message instance using the 'warn' level |
---|
| 237 | * |
---|
| 238 | * @param message |
---|
| 239 | * the message to log |
---|
| 240 | */ |
---|
| 241 | public void logWarn(Object message) { |
---|
| 242 | logInternal(WARN, message, null); |
---|
| 243 | } |
---|
| 244 | |
---|
| 245 | /** |
---|
| 246 | * Logs the given message and Throwable at the 'warn' level. |
---|
| 247 | * |
---|
| 248 | * @param message |
---|
| 249 | * the message to log |
---|
| 250 | * @param exception |
---|
| 251 | * the throwable to log (may be null) |
---|
| 252 | */ |
---|
| 253 | public void logWarn(Object message, Throwable exception) { |
---|
| 254 | logInternal(WARN, message, exception); |
---|
| 255 | } |
---|
| 256 | |
---|
| 257 | private void logInternal(int level, Object msg, Throwable exception) { |
---|
| 258 | StringBuffer msgBuf = new StringBuffer(); |
---|
| 259 | msgBuf.append(new Date().toString()); |
---|
| 260 | msgBuf.append(" "); |
---|
| 261 | |
---|
| 262 | switch (level) { |
---|
| 263 | case FATAL: |
---|
| 264 | msgBuf.append("FATAL: "); |
---|
| 265 | |
---|
| 266 | break; |
---|
| 267 | |
---|
| 268 | case ERROR: |
---|
| 269 | msgBuf.append("ERROR: "); |
---|
| 270 | |
---|
| 271 | break; |
---|
| 272 | |
---|
| 273 | case WARN: |
---|
| 274 | msgBuf.append("WARN: "); |
---|
| 275 | |
---|
| 276 | break; |
---|
| 277 | |
---|
| 278 | case INFO: |
---|
| 279 | msgBuf.append("INFO: "); |
---|
| 280 | |
---|
| 281 | break; |
---|
| 282 | |
---|
| 283 | case DEBUG: |
---|
| 284 | msgBuf.append("DEBUG: "); |
---|
| 285 | |
---|
| 286 | break; |
---|
| 287 | |
---|
| 288 | case TRACE: |
---|
| 289 | msgBuf.append("TRACE: "); |
---|
| 290 | |
---|
| 291 | break; |
---|
| 292 | } |
---|
| 293 | |
---|
| 294 | if (msg instanceof ProfilerEvent) { |
---|
| 295 | msgBuf.append(LogUtils.expandProfilerEventIfNecessary(msg)); |
---|
| 296 | |
---|
| 297 | } else { |
---|
| 298 | if (this.logLocationInfo && level != TRACE) { |
---|
| 299 | Throwable locationException = new Throwable(); |
---|
| 300 | msgBuf.append(LogUtils |
---|
| 301 | .findCallingClassAndMethod(locationException)); |
---|
| 302 | msgBuf.append(" "); |
---|
| 303 | } |
---|
| 304 | |
---|
| 305 | if (msg != null) { |
---|
| 306 | msgBuf.append(String.valueOf(msg)); |
---|
| 307 | } |
---|
| 308 | } |
---|
| 309 | |
---|
| 310 | if (exception != null) { |
---|
| 311 | msgBuf.append("\n"); |
---|
| 312 | msgBuf.append("\n"); |
---|
| 313 | msgBuf.append("EXCEPTION STACK TRACE:"); |
---|
| 314 | msgBuf.append("\n"); |
---|
| 315 | msgBuf.append("\n"); |
---|
| 316 | msgBuf.append(Util.stackTraceToString(exception)); |
---|
| 317 | } |
---|
| 318 | |
---|
| 319 | String messageAsString = msgBuf.toString(); |
---|
| 320 | |
---|
| 321 | System.err.println(messageAsString); |
---|
| 322 | |
---|
| 323 | if (bufferedLog != null) { |
---|
| 324 | bufferedLog.append(messageAsString); |
---|
| 325 | } |
---|
| 326 | } |
---|
| 327 | } |
---|