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.profiler.ProfilerEvent;
028:
029: import java.util.logging.Level;
030: import java.util.logging.Logger;
031:
032: /**
033: * Logging functionality for JDK1.4
034: *
035: * @author Mark Matthews
036: *
037: * @version $Id: Jdk14Logger.java 3726 2005-05-19 15:52:24Z mmatthews $
038: */
039: public class Jdk14Logger implements Log {
040: private static final Level DEBUG = Level.FINE;
041:
042: private static final Level ERROR = Level.SEVERE;
043:
044: private static final Level FATAL = Level.SEVERE;
045:
046: private static final Level INFO = Level.INFO;
047:
048: private static final Level TRACE = Level.FINEST;
049:
050: private static final Level WARN = Level.WARNING;
051:
052: /**
053: * The underlying logger from JDK-1.4
054: */
055: protected Logger jdkLogger = null;
056:
057: /**
058: * Creates a new Jdk14Logger object.
059: *
060: * @param name
061: * DOCUMENT ME!
062: */
063: public Jdk14Logger(String name) {
064: this .jdkLogger = Logger.getLogger(name);
065: }
066:
067: /**
068: * @see com.mysql.jdbc.log.Log#isDebugEnabled()
069: */
070: public boolean isDebugEnabled() {
071: return this .jdkLogger.isLoggable(Level.FINE);
072: }
073:
074: /**
075: * @see com.mysql.jdbc.log.Log#isErrorEnabled()
076: */
077: public boolean isErrorEnabled() {
078: return this .jdkLogger.isLoggable(Level.SEVERE);
079: }
080:
081: /**
082: * @see com.mysql.jdbc.log.Log#isFatalEnabled()
083: */
084: public boolean isFatalEnabled() {
085: return this .jdkLogger.isLoggable(Level.SEVERE);
086: }
087:
088: /**
089: * @see com.mysql.jdbc.log.Log#isInfoEnabled()
090: */
091: public boolean isInfoEnabled() {
092: return this .jdkLogger.isLoggable(Level.INFO);
093: }
094:
095: /**
096: * @see com.mysql.jdbc.log.Log#isTraceEnabled()
097: */
098: public boolean isTraceEnabled() {
099: 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(
242: StackTraceElement[] stackTrace) {
243: int numFrames = stackTrace.length;
244:
245: for (int i = 0; i < numFrames; i++) {
246: String callerClassName = stackTrace[i].getClassName();
247:
248: if (!callerClassName.startsWith("com.mysql.jdbc")
249: || callerClassName
250: .startsWith("com.mysql.jdbc.compliance")) {
251: return i;
252: }
253: }
254:
255: return 0;
256: }
257:
258: private void logInternal(Level level, Object msg,
259: Throwable exception) {
260: //
261: // only go through this exercise if the message will actually
262: // be logged.
263: //
264:
265: if (this .jdkLogger.isLoggable(level)) {
266: String messageAsString = null;
267: String callerMethodName = "N/A";
268: String callerClassName = "N/A";
269: int lineNumber = 0;
270: String fileName = "N/A";
271:
272: if (msg instanceof ProfilerEvent) {
273: messageAsString = LogUtils
274: .expandProfilerEventIfNecessary(msg).toString();
275: } else {
276: Throwable locationException = new Throwable();
277: StackTraceElement[] locations = locationException
278: .getStackTrace();
279:
280: int frameIdx = findCallerStackDepth(locations);
281:
282: if (frameIdx != 0) {
283: callerClassName = locations[frameIdx]
284: .getClassName();
285: callerMethodName = locations[frameIdx]
286: .getMethodName();
287: lineNumber = locations[frameIdx].getLineNumber();
288: fileName = locations[frameIdx].getFileName();
289: }
290:
291: messageAsString = String.valueOf(msg);
292: }
293:
294: if (exception == null) {
295: this.jdkLogger.logp(level, callerClassName,
296: callerMethodName, messageAsString);
297: } else {
298: this.jdkLogger.logp(level, callerClassName,
299: callerMethodName, messageAsString, exception);
300: }
301: }
302: }
303: }
|