001: /*
002: Copyright (C) 2002-2004 MySQL AB
003:
004: This program is free software; you can redistribute it and/or modify
005: it under the terms of version 2 of the GNU General Public License as
006: published by the Free Software Foundation.
007:
008: There are special exceptions to the terms and conditions of the GPL
009: as it is applied to this software. View the full text of the
010: exception in file EXCEPTIONS-CONNECTOR-J in the directory of this
011: software distribution.
012:
013: This program is distributed in the hope that it will be useful,
014: but WITHOUT ANY WARRANTY; without even the implied warranty of
015: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: GNU General Public License for more details.
017:
018: You should have received a copy of the GNU General Public License
019: along with this program; if not, write to the Free Software
020: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
021:
022:
023:
024: */
025: package com.mysql.jdbc.log;
026:
027: import com.mysql.jdbc.Util;
028: import com.mysql.jdbc.profiler.ProfilerEvent;
029:
030: import java.util.Date;
031:
032: /**
033: * Provides logging facilities for those platforms that don't have built-in
034: * facilities. Simply logs messages to STDERR.
035: *
036: * @author Mark Matthews
037: *
038: * @version $Id: StandardLogger.java 4597 2005-11-22 21:13:51Z mmatthews $
039: */
040: public class StandardLogger implements Log {
041: private static final int FATAL = 0;
042:
043: private static final int ERROR = 1;
044:
045: private static final int WARN = 2;
046:
047: private static final int INFO = 3;
048:
049: private static final int DEBUG = 4;
050:
051: private static final int TRACE = 5;
052:
053: public static StringBuffer bufferedLog = null;
054:
055: private boolean logLocationInfo = true;
056:
057: /**
058: * Creates a new StandardLogger object.
059: *
060: * @param name
061: * the name of the configuration to use -- ignored
062: */
063: public StandardLogger(String name) {
064: this (name, false);
065: }
066:
067: public StandardLogger(String name, boolean logLocationInfo) {
068: this .logLocationInfo = logLocationInfo;
069: }
070:
071: public static void saveLogsToBuffer() {
072: if (bufferedLog == null) {
073: bufferedLog = new StringBuffer();
074: }
075: }
076:
077: /**
078: * @see com.mysql.jdbc.log.Log#isDebugEnabled()
079: */
080: public boolean isDebugEnabled() {
081: return true;
082: }
083:
084: /**
085: * @see com.mysql.jdbc.log.Log#isErrorEnabled()
086: */
087: public boolean isErrorEnabled() {
088: return true;
089: }
090:
091: /**
092: * @see com.mysql.jdbc.log.Log#isFatalEnabled()
093: */
094: public boolean isFatalEnabled() {
095: return true;
096: }
097:
098: /**
099: * @see com.mysql.jdbc.log.Log#isInfoEnabled()
100: */
101: public boolean isInfoEnabled() {
102: return true;
103: }
104:
105: /**
106: * @see com.mysql.jdbc.log.Log#isTraceEnabled()
107: */
108: public boolean isTraceEnabled() {
109: return true;
110: }
111:
112: /**
113: * @see com.mysql.jdbc.log.Log#isWarnEnabled()
114: */
115: public boolean isWarnEnabled() {
116: return true;
117: }
118:
119: /**
120: * Logs the given message instance using the 'debug' level
121: *
122: * @param message
123: * the message to log
124: */
125: public void logDebug(Object message) {
126: logInternal(DEBUG, message, null);
127: }
128:
129: /**
130: * Logs the given message and Throwable at the 'debug' level.
131: *
132: * @param message
133: * the message to log
134: * @param exception
135: * the throwable to log (may be null)
136: */
137: public void logDebug(Object message, Throwable exception) {
138: logInternal(DEBUG, message, exception);
139: }
140:
141: /**
142: * Logs the given message instance using the 'error' level
143: *
144: * @param message
145: * the message to log
146: */
147: public void logError(Object message) {
148: logInternal(ERROR, message, null);
149: }
150:
151: /**
152: * Logs the given message and Throwable at the 'error' level.
153: *
154: * @param message
155: * the message to log
156: * @param exception
157: * the throwable to log (may be null)
158: */
159: public void logError(Object message, Throwable exception) {
160: logInternal(ERROR, message, exception);
161: }
162:
163: /**
164: * Logs the given message instance using the 'fatal' level
165: *
166: * @param message
167: * the message to log
168: */
169: public void logFatal(Object message) {
170: logInternal(FATAL, message, null);
171: }
172:
173: /**
174: * Logs the given message and Throwable at the 'fatal' level.
175: *
176: * @param message
177: * the message to log
178: * @param exception
179: * the throwable to log (may be null)
180: */
181: public void logFatal(Object message, Throwable exception) {
182: logInternal(FATAL, message, exception);
183: }
184:
185: /**
186: * Logs the given message instance using the 'info' level
187: *
188: * @param message
189: * the message to log
190: */
191: public void logInfo(Object message) {
192: logInternal(INFO, message, null);
193: }
194:
195: /**
196: * Logs the given message and Throwable at the 'info' level.
197: *
198: * @param message
199: * the message to log
200: * @param exception
201: * the throwable to log (may be null)
202: */
203: public void logInfo(Object message, Throwable exception) {
204: logInternal(INFO, message, exception);
205: }
206:
207: /**
208: * Logs the given message instance using the 'trace' level
209: *
210: * @param message
211: * the message to log
212: */
213: public void logTrace(Object message) {
214: logInternal(TRACE, message, null);
215: }
216:
217: /**
218: * Logs the given message and Throwable at the 'trace' level.
219: *
220: * @param message
221: * the message to log
222: * @param exception
223: * the throwable to log (may be null)
224: */
225: public void logTrace(Object message, Throwable exception) {
226: logInternal(TRACE, message, exception);
227: }
228:
229: /**
230: * Logs the given message instance using the 'warn' level
231: *
232: * @param message
233: * the message to log
234: */
235: public void logWarn(Object message) {
236: logInternal(WARN, message, null);
237: }
238:
239: /**
240: * Logs the given message and Throwable at the 'warn' level.
241: *
242: * @param message
243: * the message to log
244: * @param exception
245: * the throwable to log (may be null)
246: */
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 {
292: if (this .logLocationInfo && level != TRACE) {
293: Throwable locationException = new Throwable();
294: msgBuf.append(LogUtils
295: .findCallingClassAndMethod(locationException));
296: msgBuf.append(" ");
297: }
298:
299: if (msg != null) {
300: msgBuf.append(String.valueOf(msg));
301: }
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: }
|