KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javolution > util > StandardLog


1 /*
2  * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
3  * Copyright (C) 2006 - Javolution (http://javolution.org/)
4  * All rights reserved.
5  *
6  * Permission to use, copy, modify, and distribute this software is
7  * freely granted, provided that this notice is preserved.
8  */

9 package javolution.util;
10
11 import javolution.Javolution;
12 import javolution.context.LogContext;
13 import j2me.lang.CharSequence;
14 import j2me.util.logging.Level;
15 import j2me.util.logging.Logger;
16 import j2me.util.logging.LogRecord;
17
18 /**
19  * <p> This class represents a specialized logging context forwarding events
20  * to a standard logger (<code>java.util.logging.Logger</code>).</p>
21  *
22  * <p> This class leverages the capabilities of the standard logging facility
23  * and extends it to support specialized {@link LogContext logging} on a
24  * thread or object basis. For example:[code]
25  * StandardLog remoteLog = new StandardLog(Logger.getLogger("remote"));
26  * StandardLog.enter(remoteLog);
27  * try {
28  * StandardLog.fine("Current thread uses a remote logger");
29  * ...
30  * } finally {
31  * StandardLog.exit(remoteLog); // Reverts to previous logging context.
32  * }[/code]</p>
33  *
34  *
35  * @author <a HREF="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
36  * @version 3.6, November 16, 2005
37  */

38 public class StandardLog extends LogContext {
39
40     /**
41      * The logger associated to this logging context.
42      */

43     private Logger _logger;
44
45     /**
46      * Creates a logging context forwarding events to the root logger
47      * (<code>Logger.getLogger("")</code>).
48      */

49     public StandardLog() {
50         this(Logger.getLogger(""));
51     }
52
53     /**
54      * Creates a standard log context forwarding events to the specified
55      * logger.
56      *
57      * @param logger the logger to which log events are forwarded to.
58      */

59     public StandardLog(Logger logger) {
60         _logger = logger;
61     }
62
63     /**
64      * Returns the logger to which this context forwards the events to.
65      *
66      * @return the logger for this standard logging context.
67      */

68     public final Logger getLogger() {
69         return _logger;
70     }
71
72     /**
73      * Checks if a message of the given level would actually be logged
74      * by this logger.
75      *
76      * @param level the message logging level
77      * @return <code>true</code> if a message of specified level would actually
78      * be logged;<code>false</code> otherwise.
79      */

80     public static boolean isLoggable(Level level) {
81         LogContext log = (LogContext) LogContext.current();
82         if (log instanceof StandardLog) {
83             return ((StandardLog) log)._logger.isLoggable(level);
84         } else if (level.intValue() >= Level.WARNING.intValue()) {
85             return log.isWarningLogged();
86         } else if (level.intValue() >= Level.INFO.intValue()) {
87             return log.isInfoLogged();
88         } else {
89             return false;
90         }
91     }
92
93     /**
94      * Log a specific LogRecord. If the current logging context is not a
95      * {@link StandardLog}, an {@link LogContext#logError error},
96      * {@link LogContext#logWarning warning} or
97      * {@link LogContext#logInfo info} is possibly logged.
98      *
99      * @param record the LogRecord to be published.
100      */

101     public static void log(LogRecord record) {
102         LogContext log = (LogContext) LogContext.current();
103         if (log instanceof StandardLog) {
104             ((StandardLog) log)._logger.log(record);
105         } else {
106             Throwable JavaDoc error = record.getThrown();
107             if (error != null) {
108                 log.logError(error, toCsq(record.getMessage()));
109             } else if (record.getLevel().intValue() > Level.WARNING.intValue()) {
110                 log.logWarning(toCsq(record.getMessage()));
111             } else if (record.getLevel().intValue() > Level.INFO.intValue()) {
112                 log.logInfo(toCsq(record.getMessage()));
113             }
114         }
115     }
116
117     /**
118      * Logs a {@link Level#SEVERE SEVERE} message. If the current logging
119      * context is not a {@link StandardLog} a {@link LogContext#logWarning
120      * warning} message is logged.
121      *
122      * @param msg the severe message.
123      */

124     public static void severe(String JavaDoc msg) {
125         LogContext log = (LogContext) LogContext.current();
126         if (log instanceof StandardLog) {
127             ((StandardLog) log)._logger.severe(msg);
128         } else {
129             log.logWarning(toCsq(msg));
130         }
131     }
132
133     /**
134      * Logs a {@link Level#WARNING WARNING} message.
135      *
136      * @param msg the warning message.
137      * @see LogContext#logWarning(CharSequence)
138      */

139     public static void warning(String JavaDoc msg) {
140         LogContext log = (LogContext) LogContext.current();
141         log.logWarning(toCsq(msg));
142     }
143
144     /**
145      * Logs an {@link Level#INFO INFO} message.
146      *
147      * @param msg the informative message.
148      * @see LogContext#logInfo(CharSequence)
149      */

150     public static void info(String JavaDoc msg) {
151         LogContext log = (LogContext) LogContext.current();
152         log.logInfo(toCsq(msg));
153     }
154
155     /**
156      * Logs a {@link Level#CONFIG CONFIG} message. If the current logging
157      * context is not a {@link StandardLog} no message is logged.
158      *
159      * @param msg the config message.
160      */

161     public static void config(String JavaDoc msg) {
162         LogContext log = (LogContext) LogContext.current();
163         if (log instanceof StandardLog) {
164             ((StandardLog) log)._logger.config(msg);
165         }
166     }
167
168     /**
169      * Logs a {@link Level#FINE FINE} message. If the current logging
170      * context is not a {@link StandardLog} no message is logged.
171      *
172      * @param msg the fine message.
173      */

174     public static void fine(String JavaDoc msg) {
175         LogContext log = (LogContext) LogContext.current();
176         if (log instanceof StandardLog) {
177             ((StandardLog) log)._logger.fine(msg);
178         }
179     }
180
181     /**
182      * Logs a {@link Level#FINER FINER} message. If the current logging
183      * context is not a {@link StandardLog} no message is logged.
184      *
185      * @param msg the finer message.
186      */

187     public static void finer(String JavaDoc msg) {
188         LogContext log = (LogContext) LogContext.current();
189         if (log instanceof StandardLog) {
190             ((StandardLog) log)._logger.finer(msg);
191         }
192     }
193
194     /**
195      * Logs a {@link Level#FINEST FINEST} message. If the current logging
196      * context is not a {@link StandardLog} no message is logged.
197      *
198      * @param msg the finest message.
199      */

200     public static void finest(String JavaDoc msg) {
201         LogContext log = (LogContext) LogContext.current();
202         if (log instanceof StandardLog) {
203             ((StandardLog) log)._logger.finest(msg);
204         }
205     }
206
207     /**
208      * Logs throwing an exception. If the current logging context is not a
209      * {@link StandardLog} an {@link LogContext#logError error} is logged.
210      *
211      * @param sourceClass name of class that issued the logging request.
212      * @param sourceMethod name of the method.
213      * @param thrown the error that is being thrown.
214      */

215     public static void throwing(String JavaDoc sourceClass, String JavaDoc sourceMethod,
216             Throwable JavaDoc thrown) {
217         LogContext log = (LogContext) LogContext.current();
218         if (log instanceof StandardLog) {
219             ((StandardLog) log)._logger.throwing(sourceClass, sourceMethod,
220                     thrown);
221         } else {
222             log.logError(thrown, null);
223         }
224     }
225
226     /**
227      * Log a method entry. If the current logging context is not a
228      * {@link StandardLog} no entry is logged.
229      *
230      * @param sourceClass name of class that issued the logging request.
231      * @param sourceMethod name of method that is being entered.
232      */

233     public static void entering(String JavaDoc sourceClass, String JavaDoc sourceMethod) {
234         LogContext log = (LogContext) LogContext.current();
235         if (log instanceof StandardLog) {
236             ((StandardLog) log)._logger.entering(sourceClass, sourceMethod);
237         }
238     }
239
240     /**
241      * Log a method return. If the current logging context is not a
242      * {@link StandardLog} no return is logged.
243      *
244      * @param sourceClass name of class that issued the logging request.
245      * @param sourceMethod name of method that is being returned.
246      */

247     public static void exiting(String JavaDoc sourceClass, String JavaDoc sourceMethod) {
248         LogContext log = (LogContext) LogContext.current();
249         if (log instanceof StandardLog) {
250             ((StandardLog) log)._logger.exiting(sourceClass, sourceMethod);
251         }
252     }
253
254     // Implements LogContext abstract method.
255
public boolean isInfoLogged() {
256         return _logger.isLoggable(Level.INFO);
257     }
258
259     // Implements LogContext abstract method.
260
public boolean isWarningLogged() {
261         return _logger.isLoggable(Level.WARNING);
262     }
263
264     // Implements LogContext abstract method.
265
public boolean isErrorLogged() {
266         return _logger.isLoggable(Level.SEVERE);
267     }
268
269     // Implements LogContext abstract method.
270
public void logInfo(CharSequence JavaDoc message) {
271         _logger.info(message.toString());
272     }
273
274     // Implements LogContext abstract method.
275
public void logWarning(CharSequence JavaDoc message) {
276         _logger.warning(message.toString());
277     }
278
279     // Implements LogContext abstract method.
280
public void logError(Throwable JavaDoc error, CharSequence JavaDoc message) {
281         String JavaDoc msg = (message == null) ? error.getMessage() : message
282                 .toString();
283         msg = (msg == null) ? error.getClass().getName() : error.getClass()
284                 .getName()
285                 + " - " + msg;
286         _logger.severe(msg);
287     }
288
289     private static CharSequence JavaDoc toCsq/**/(Object JavaDoc str) {
290         return Javolution.j2meToCharSeq(str);
291     }
292 }
Popular Tags