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.Util; |
28 | import com.mysql.jdbc.profiler.ProfilerEvent; |
29 | |
30 | import java.util.Date; |
31 | |
32 | /** |
33 | * Provides logging facilities for those platforms that don't have built-in |
34 | * facilities. Simply logs messages to STDERR. |
35 | * |
36 | * @author Mark Matthews |
37 | * |
38 | * @version $Id: StandardLogger.java 4596 2005-11-22 15:12:36 -0600 (Tue, 22 Nov 2005) mmatthews $ |
39 | */ |
40 | public class StandardLogger implements Log { |
41 | private static final int FATAL = 0; |
42 | |
43 | private static final int ERROR = 1; |
44 | |
45 | private static final int WARN = 2; |
46 | |
47 | private static final int INFO = 3; |
48 | |
49 | private static final int DEBUG = 4; |
50 | |
51 | private static final int TRACE = 5; |
52 | |
53 | public static StringBuffer bufferedLog = null; |
54 | |
55 | private boolean logLocationInfo = true; |
56 | |
57 | /** |
58 | * Creates a new StandardLogger object. |
59 | * |
60 | * @param name |
61 | * the name of the configuration to use -- ignored |
62 | */ |
63 | public StandardLogger(String name) { |
64 | this(name, true); |
65 | } |
66 | |
67 | public StandardLogger(String name, boolean logLocationInfo) { |
68 | this.logLocationInfo = logLocationInfo; |
69 | } |
70 | |
71 | public static void saveLogsToBuffer() { |
72 | if (bufferedLog == null) { |
73 | bufferedLog = new StringBuffer(); |
74 | } |
75 | } |
76 | |
77 | /** |
78 | * @see com.mysql.jdbc.log.Log#isDebugEnabled() |
79 | */ |
80 | public boolean isDebugEnabled() { |
81 | return true; |
82 | } |
83 | |
84 | /** |
85 | * @see com.mysql.jdbc.log.Log#isErrorEnabled() |
86 | */ |
87 | public boolean isErrorEnabled() { |
88 | return true; |
89 | } |
90 | |
91 | /** |
92 | * @see com.mysql.jdbc.log.Log#isFatalEnabled() |
93 | */ |
94 | public boolean isFatalEnabled() { |
95 | return true; |
96 | } |
97 | |
98 | /** |
99 | * @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 | } |