KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > grlea > logBridge > impl > J2SELogBridgeFactory


1 package org.grlea.logBridge.impl;
2
3 // $Id: J2SELogBridgeFactory.java,v 1.2 2005/04/27 22:25:08 grlea Exp $
4
// Copyright (c) 2004 Graham Lea. All rights reserved.
5

6 // Licensed under the Apache License, Version 2.0 (the "License");
7
// you may not use this file except in compliance with the License.
8
// You may obtain a copy of the License at
9
//
10
// http://www.apache.org/licenses/LICENSE-2.0
11
//
12
// Unless required by applicable law or agreed to in writing, software
13
// distributed under the License is distributed on an "AS IS" BASIS,
14
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
// See the License for the specific language governing permissions and
16
// limitations under the License.
17

18 import org.grlea.logBridge.LogBridge;
19 import org.grlea.logBridge.LogBridgeFactory;
20
21 import java.util.logging.Level JavaDoc;
22 import java.util.logging.Logger JavaDoc;
23 import java.util.logging.LogRecord JavaDoc;
24
25 /**
26  * <p>A {@link LogBridgeFactory} implementation for the
27  * <a HREF="http://java.sun.com/j2se/1.4.2/docs/api/java/util/logging/package-summary.html"
28  * target="_blank">
29  * J2SE Logging API</a>.</p>
30  *
31  * <p>Log Bridge levels convert to J2SE's levels as follows:
32  * <table>
33  * <tr><th>Log Bridge</th> <th>J2SE Level</th></tr>
34  * <tr><td>Error</td> <td>{@link Level#SEVERE}</td></tr>
35  * <tr><td>Warning</td> <td>{@link Level#WARNING}</td></tr>
36  * <tr><td>Info</td> <td>{@link Level#INFO}</td></tr>
37  * <tr><td>Debug</td> <td>{@link Level#FINE}</td></tr>
38  * <tr><td>Verbose</td> <td>{@link Level#FINER}</td></tr>
39  * <tr><td>Tracing</td> <td>{@link Level#FINEST}</td></tr>
40  * </table>
41  * </p>
42  *
43  * @author grlea
44  * @version $Revision: 1.2 $
45  */

46 public class
47 J2SELogBridgeFactory
48 implements LogBridgeFactory
49 {
50    public
51    J2SELogBridgeFactory()
52    {
53    }
54
55    public LogBridge
56    getLogBridge(Class JavaDoc sourceClass)
57    {
58       return new Bridge(Logger.getLogger(sourceClass.getName()));
59    }
60
61    private static final class
62    Bridge
63    implements LogBridge
64    {
65       private static final Level JavaDoc ERROR = Level.SEVERE;
66       private static final Level JavaDoc WARN = Level.WARNING;
67       private static final Level JavaDoc INFO = Level.INFO;
68       private static final Level JavaDoc DEBUG = Level.FINE;
69       private static final Level JavaDoc VERBOSE = Level.FINER;
70       private static final Level JavaDoc TRACE = Level.FINEST;
71
72       private final Logger JavaDoc logger;
73
74       public
75       Bridge(Logger JavaDoc logger)
76       {
77          this.logger = logger;
78       }
79
80       public boolean
81       isErrorEnabled()
82       {
83          return logger.isLoggable(ERROR);
84       }
85
86       public boolean
87       isWarnEnabled()
88       {
89          return logger.isLoggable(WARN);
90       }
91
92       public boolean
93       isInfoEnabled()
94       {
95          return logger.isLoggable(INFO);
96       }
97
98       public boolean
99       isDebugEnabled()
100       {
101          return logger.isLoggable(DEBUG);
102       }
103
104       public boolean
105       isVerboseEnabled()
106       {
107          return logger.isLoggable(VERBOSE);
108       }
109
110       public boolean
111       isTracingEnabled()
112       {
113          return logger.isLoggable(TRACE);
114       }
115
116       public void
117       error(String JavaDoc message)
118       {
119          logMessage(ERROR, message);
120       }
121
122       public void
123       error(String JavaDoc objectName, Object JavaDoc value)
124       {
125          logObject(ERROR, objectName, value);
126       }
127
128       public void
129       error(Throwable JavaDoc error)
130       {
131          logThrowable(ERROR, error);
132       }
133
134       public void
135       warn(String JavaDoc message)
136       {
137             logMessage(WARN, message);
138       }
139
140       public void
141       warn(String JavaDoc objectName, Object JavaDoc value)
142       {
143          logObject(WARN, objectName, value);
144       }
145
146       public void
147       warn(Throwable JavaDoc error)
148       {
149          logThrowable(WARN, error);
150       }
151
152       public void
153       info(String JavaDoc message)
154       {
155          logMessage(INFO, message);
156       }
157
158       public void
159       info(String JavaDoc objectName, Object JavaDoc value)
160       {
161          logObject(INFO, objectName, value);
162       }
163
164       public void
165       info(Throwable JavaDoc error)
166       {
167          logThrowable(INFO, error);
168       }
169
170       public void
171       debug(String JavaDoc message)
172       {
173          logMessage(DEBUG, message);
174       }
175
176       public void
177       debug(String JavaDoc objectName, Object JavaDoc value)
178       {
179          logObject(DEBUG, objectName, value);
180       }
181
182       public void
183       debug(Throwable JavaDoc error)
184       {
185          logThrowable(DEBUG, error);
186       }
187
188       public void
189       verbose(String JavaDoc message)
190       {
191          logMessage(VERBOSE, message);
192       }
193
194       public void
195       verbose(String JavaDoc objectName, Object JavaDoc value)
196       {
197          logObject(VERBOSE, objectName, value);
198       }
199
200       public void
201       verbose(Throwable JavaDoc error)
202       {
203          logThrowable(VERBOSE, error);
204       }
205
206       public void
207       entry(String JavaDoc methodName)
208       {
209          logger.entering(logger.getName(), methodName);
210       }
211
212       public void
213       exit(String JavaDoc methodName)
214       {
215          logger.exiting(logger.getName(), methodName);
216       }
217
218       /**
219        * Logs a message at the specified level.
220        *
221        * @param level the level at which the message is to be logged
222        * @param message the message to log
223        */

224       private void
225       logMessage(Level JavaDoc level, String JavaDoc message)
226       {
227          if (logger.isLoggable(level))
228             logger.log(correctLogSource(new LogRecord JavaDoc(level, message), 2));
229       }
230
231       /**
232        * Logs an object name and value at the specified level.
233        *
234        * @param level the level at which the message is to be logged
235        * @param objectName the name of the object being logged
236        * @param value the value of the object
237        */

238       private void
239       logObject(Level JavaDoc level, String JavaDoc objectName, Object JavaDoc value)
240       {
241          if (logger.isLoggable(level))
242          {
243             LogRecord JavaDoc record = correctLogSource(new LogRecord JavaDoc(level, objectName + ": {0}"), 2);
244             record.setParameters(new Object JavaDoc[] {value});
245             logger.log(record);
246          }
247       }
248
249       /**
250        * Logs a Throwable at the specified level.
251        *
252        * @param level the level at which the message is to be logged
253        * @param error the throwable to be logged
254        */

255       private void
256       logThrowable(Level JavaDoc level, Throwable JavaDoc error)
257       {
258          if (logger.isLoggable(level))
259          {
260             LogRecord JavaDoc record = correctLogSource(new LogRecord JavaDoc(level, ""), 2);
261             record.setThrown(error);
262             logger.log(record);
263          }
264       }
265
266       /**
267        * <p>Corrects the {@link LogRecord#getSourceClassName source class} and
268        * {@link LogRecord#getSourceMethodName source method} names in the given log record to
269        * contain the names of the class and method that called this <code>Bridge</code>, rather
270        * than those of the bridge itself.</p>
271        *
272        * <p>Note that the information necessary may not always be available, and in such cases the
273        * string "Unknown" is used instead.</p>
274        *
275        * @param log the record to correct
276        * @param depth the depth (number of method calls removed from the log bridge client) from
277        * which this method is being called.
278        *
279        * @return the same LogRecord instance, after its names have been corrected.
280        */

281       private LogRecord JavaDoc
282       correctLogSource(LogRecord JavaDoc log, int depth)
283       {
284          StackTraceElement JavaDoc[] stackTrace = new Throwable JavaDoc().getStackTrace();
285
286          String JavaDoc logSourceClassName = null;
287          String JavaDoc logSourceMethodName = null;
288
289          int logSourceFrame = depth + 1;
290          if (logSourceFrame < stackTrace.length)
291          {
292             StackTraceElement JavaDoc logSource = stackTrace[logSourceFrame];
293             if (logSource != null)
294             {
295                logSourceClassName = logSource.getClassName();
296                logSourceMethodName = logSource.getMethodName();
297             }
298          }
299
300          log.setSourceClassName(logSourceClassName == null ? "Unknown" : logSourceClassName);
301          log.setSourceMethodName(logSourceMethodName == null ? "Unknown" : logSourceMethodName);
302
303          return log;
304       }
305    }
306 }
Popular Tags