2
Copyright (C) 2002-2004 MySQL AB
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.
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.
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.
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
25
package com.mysql.jdbc.log;
27
import com.mysql.jdbc.Util;
28
import com.mysql.jdbc.profiler.ProfilerEvent;
30
import java.util.Date;
33
* Provides logging facilities for those platforms that don't have built-in
34
* facilities. Simply logs messages to STDERR.
36
* @author Mark Matthews
38
* @version $Id: StandardLogger.java 4597 2005-11-22 21:13:51Z mmatthews $
40
public class StandardLogger implements Log {
41
private static final int FATAL = 0;
43
private static final int ERROR = 1;
45
private static final int WARN = 2;
47
private static final int INFO = 3;
49
private static final int DEBUG = 4;
51
private static final int TRACE = 5;
53
public static StringBuffer bufferedLog = null;
55
private boolean logLocationInfo = true;
58
* Creates a new StandardLogger object.
61
* the name of the configuration to use -- ignored
63
public StandardLogger(String name) {
67
public StandardLogger(String name, boolean logLocationInfo) {
68
this.logLocationInfo = logLocationInfo;
71
public static void saveLogsToBuffer() {
72
if (bufferedLog == null) {
73
bufferedLog = new StringBuffer();
78
* @see com.mysql.jdbc.log.Log#isDebugEnabled()
80
public boolean isDebugEnabled() {
85
* @see com.mysql.jdbc.log.Log#isErrorEnabled()
87
public boolean isErrorEnabled() {
92
* @see com.mysql.jdbc.log.Log#isFatalEnabled()
94
public boolean isFatalEnabled() {
99
* @see com.mysql.jdbc.log.Log#isInfoEnabled()
101
public boolean isInfoEnabled() {
106
* @see com.mysql.jdbc.log.Log#isTraceEnabled()
108
public boolean isTraceEnabled() {
113
* @see com.mysql.jdbc.log.Log#isWarnEnabled()
115
public boolean isWarnEnabled() {
120
* Logs the given message instance using the 'debug' level
125
public void logDebug(Object message) {
126
logInternal(DEBUG, message, null);
130
* Logs the given message and Throwable at the 'debug' level.
135
* the throwable to log (may be null)
137
public void logDebug(Object message, Throwable exception) {
138
logInternal(DEBUG, message, exception);
142
* Logs the given message instance using the 'error' level
147
public void logError(Object message) {
148
logInternal(ERROR, message, null);
152
* Logs the given message and Throwable at the 'error' level.
157
* the throwable to log (may be null)
159
public void logError(Object message, Throwable exception) {
160
logInternal(ERROR, message, exception);
164
* Logs the given message instance using the 'fatal' level
169
public void logFatal(Object message) {
170
logInternal(FATAL, message, null);
174
* Logs the given message and Throwable at the 'fatal' level.
179
* the throwable to log (may be null)
181
public void logFatal(Object message, Throwable exception) {
182
logInternal(FATAL, message, exception);
186
* Logs the given message instance using the 'info' level
191
public void logInfo(Object message) {
192
logInternal(INFO, message, null);
196
* Logs the given message and Throwable at the 'info' level.
201
* the throwable to log (may be null)
203
public void logInfo(Object message, Throwable exception) {
204
logInternal(INFO, message, exception);
208
* Logs the given message instance using the 'trace' level
213
public void logTrace(Object message) {
214
logInternal(TRACE, message, null);
218
* Logs the given message and Throwable at the 'trace' level.
223
* the throwable to log (may be null)
225
public void logTrace(Object message, Throwable exception) {
226
logInternal(TRACE, message, exception);
230
* Logs the given message instance using the 'warn' level
235
public void logWarn(Object message) {
236
logInternal(WARN, message, null);
240
* Logs the given message and Throwable at the 'warn' level.
245
* the throwable to log (may be null)
247
public void logWarn(Object message, Throwable exception) {
248
logInternal(WARN, message, exception);
251
private void logInternal(int level, Object msg, Throwable exception) {
252
StringBuffer msgBuf = new StringBuffer();
253
msgBuf.append(new Date().toString());
258
msgBuf.append("FATAL: ");
263
msgBuf.append("ERROR: ");
268
msgBuf.append("WARN: ");
273
msgBuf.append("INFO: ");
278
msgBuf.append("DEBUG: ");
283
msgBuf.append("TRACE: ");
288
if (msg instanceof ProfilerEvent) {
289
msgBuf.append(LogUtils.expandProfilerEventIfNecessary(msg));
292
if (this.logLocationInfo && level != TRACE) {
293
Throwable locationException = new Throwable();
294
msgBuf.append(LogUtils
295
.findCallingClassAndMethod(locationException));
300
msgBuf.append(String.valueOf(msg));
304
if (exception != null) {
307
msgBuf.append("EXCEPTION STACK TRACE:");
310
msgBuf.append(Util.stackTraceToString(exception));
313
String messageAsString = msgBuf.toString();
315
System.err.println(messageAsString);
317
if (bufferedLog != null) {
318
bufferedLog.append(messageAsString);