KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mysql > jdbc > log > Jdk14Logger


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 JavaDoc;
30 import java.util.logging.Logger JavaDoc;
31
32 /**
33  * Logging functionality for JDK1.4
34  *
35  * @author Mark Matthews
36  *
37  * @version $Id: Jdk14Logger.java,v 1.1.2.2 2005/05/19 15:52:24 mmatthews Exp $
38  */

39 public class Jdk14Logger implements Log {
40     private static final Level JavaDoc DEBUG = Level.FINE;
41
42     private static final Level JavaDoc ERROR = Level.SEVERE;
43
44     private static final Level JavaDoc FATAL = Level.SEVERE;
45
46     private static final Level JavaDoc INFO = Level.INFO;
47
48     private static final Level JavaDoc TRACE = Level.FINEST;
49
50     private static final Level JavaDoc WARN = Level.WARNING;
51
52     /**
53      * The underlying logger from JDK-1.4
54      */

55     protected Logger JavaDoc jdkLogger = null;
56
57     /**
58      * Creates a new Jdk14Logger object.
59      *
60      * @param name
61      * DOCUMENT ME!
62      */

63     public Jdk14Logger(String JavaDoc 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 JavaDoc 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 JavaDoc message, Throwable JavaDoc 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 JavaDoc 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 JavaDoc message, Throwable JavaDoc 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 JavaDoc 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 JavaDoc message, Throwable JavaDoc 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 JavaDoc 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 JavaDoc message, Throwable JavaDoc 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 JavaDoc 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 JavaDoc message, Throwable JavaDoc 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 JavaDoc 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 JavaDoc message, Throwable JavaDoc exception) {
238         logInternal(WARN, message, exception);
239     }
240
241     private static final int findCallerStackDepth(StackTraceElement JavaDoc[] stackTrace) {
242         int numFrames = stackTrace.length;
243
244         for (int i = 0; i < numFrames; i++) {
245             String JavaDoc 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 JavaDoc level, Object JavaDoc msg, Throwable JavaDoc 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 JavaDoc messageAsString = null;
264             String JavaDoc callerMethodName = "N/A";
265             String JavaDoc callerClassName = "N/A";
266             int lineNumber = 0;
267             String JavaDoc fileName = "N/A";
268
269             if (msg instanceof ProfilerEvent) {
270                 messageAsString = LogUtils.expandProfilerEventIfNecessary(msg)
271                         .toString();
272             } else {
273                 Throwable JavaDoc locationException = new Throwable JavaDoc();
274                 StackTraceElement JavaDoc[] 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 }
299
Popular Tags