View Javadoc

1   /*
2    * Copyright (c) 2004 UNINETT FAS
3    *
4    * This program is free software; you can redistribute it and/or modify it
5    * under the terms of the GNU General Public License as published by the Free
6    * Software Foundation; either version 2 of the License, or (at your option)
7    * any later version.
8    *
9    * This program is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11   * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12   * more details.
13   *
14   * You should have received a copy of the GNU General Public License along with
15   * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16   * Place - Suite 330, Boston, MA 02111-1307, USA.
17   *
18   * $Id: MessageLogger.java,v 1.12 2006/02/14 15:16:06 catoolsen Exp $
19   *
20   */
21  
22  package no.feide.moria.log;
23  
24  import java.io.ByteArrayOutputStream;
25  import java.io.OutputStream;
26  import java.io.PrintStream;
27  import java.io.Serializable;
28  
29  import org.apache.log4j.Logger;
30  import org.apache.log4j.Level;
31  import org.apache.log4j.Priority;
32  
33  /***
34   * Logs generic messages from the system, may include ticket id and/or
35   * throwable. Supports four loglevels: Debug, Info, Warn, Critical.
36   * @author Bjørn Ola Smievoll <b.o@smievoll.no>
37   * @version $Revision: 1.12 $
38   */
39  public final class MessageLogger
40  implements Serializable {
41  
42      /***
43       * Serial version UID.
44       */
45      private static final long serialVersionUID = 7948139732249969470L;
46  
47      /***
48       * Log to this logger. Transient so the class can be serialized.
49       */
50      private transient Logger logger = null;
51  
52      /*** Class using this logger instance. */
53      private Class callingClass;
54  
55      /*** The current log level. Intitalized to 'ALL' */
56      private int logLevel = Level.ALL_INT;
57  
58  
59      /***
60       * Default constructor.
61       * @param callingClass
62       *            The class that will use this logger instance.
63       */
64      public MessageLogger(final Class callingClass) {
65  
66          this.callingClass = callingClass;
67          logger = Logger.getLogger(callingClass);
68  
69          /* Cache the log level for the logger. */
70          Level level = logger.getEffectiveLevel();
71  
72          if (level != null)
73              logLevel = level.toInt();
74      }
75  
76  
77      /***
78       * Logs a message with level critical.
79       * @param message
80       *            The message to log.
81       */
82      public void logCritical(final String message) {
83  
84          if (Level.FATAL_INT >= logLevel)
85              getLogger().fatal(generateLogMessage(message, null, null));
86      }
87  
88  
89      /***
90       * Logs a message with level critical.
91       * @param message
92       *            The message to log.
93       * @param throwable
94       *            An throwable associated with this log entry. Message and
95       *            stacktrace will be logged.
96       */
97      public void logCritical(final String message, final Throwable throwable) {
98  
99          if (Level.FATAL_INT >= logLevel)
100             getLogger().fatal(generateLogMessage(message, null, throwable));
101     }
102 
103 
104     /***
105      * Logs a message with level critical including ticket id.
106      * @param message
107      *            The message to log.
108      * @param ticketId
109      *            The ticket id associated with this log message.
110      */
111     public void logCritical(final String message, final String ticketId) {
112 
113         if (Level.FATAL_INT >= logLevel)
114             getLogger().fatal(generateLogMessage(message, ticketId, null));
115     }
116 
117 
118     /***
119      * Logs a message with level critical including ticket id.
120      * @param message
121      *            The message to log.
122      * @param ticketId
123      *            The ticket id associated with this log message.
124      * @param throwable
125      *            An throwable associated with this log entry. Message and
126      *            stacktrace will be logged.
127      */
128     public void logCritical(final String message,
129                             final String ticketId,
130                             final Throwable throwable) {
131 
132         if (Level.FATAL_INT >= logLevel)
133             getLogger().fatal(generateLogMessage(message, ticketId, throwable));
134     }
135 
136 
137     /***
138      * Logs a message with level warn.
139      * @param message
140      *            The message to log.
141      */
142     public void logWarn(final String message) {
143 
144         if (Level.WARN_INT >= logLevel)
145             getLogger().warn(generateLogMessage(message, null, null));
146     }
147 
148 
149     /***
150      * Logs a message with level warn.
151      * @param message
152      *            The message to log.
153      * @param throwable
154      *            An throwable associated with this log entry. Message and
155      *            stacktrace will be logged.
156      */
157     public void logWarn(final String message, final Throwable throwable) {
158 
159         if (Level.WARN_INT >= logLevel)
160             getLogger().warn(generateLogMessage(message, null, throwable));
161     }
162 
163 
164     /***
165      * Logs a message with level warn including ticket id.
166      * @param message
167      *            The message to log.
168      * @param ticketId
169      *            The ticket id associated with this log message.
170      */
171     public void logWarn(final String message, final String ticketId) {
172 
173         if (Level.WARN_INT >= logLevel)
174             getLogger().warn(generateLogMessage(message, ticketId, null));
175     }
176 
177 
178     /***
179      * Logs a message with level warn including ticket id.
180      * @param message
181      *            The message to log.
182      * @param ticketId
183      *            The ticket id associated with this log message.
184      * @param throwable
185      *            An throwable associated with this log entry. Message and
186      *            stacktrace will be logged.
187      */
188     public void logWarn(final String message,
189                         final String ticketId,
190                         final Throwable throwable) {
191 
192         if (Level.WARN_INT >= logLevel)
193             getLogger().warn(generateLogMessage(message, ticketId, throwable));
194     }
195 
196 
197     /***
198      * Logs a message with level info.
199      * @param message
200      *            The message to log.
201      */
202     public void logInfo(final String message) {
203 
204         if (Level.INFO_INT >= logLevel)
205             getLogger().info(generateLogMessage(message, null, null));
206     }
207 
208 
209     /***
210      * Logs a message with level info.
211      * @param message
212      *            The message to log.
213      * @param throwable
214      *            An throwable associated with this log entry. Message and
215      *            stacktrace will be logged.
216      */
217     public void logInfo(final String message, final Throwable throwable) {
218 
219         if (Level.INFO_INT >= logLevel)
220             getLogger().info(generateLogMessage(message, null, throwable));
221     }
222 
223 
224     /***
225      * Logs a message with level info including ticket id.
226      * @param message
227      *            The message to log.
228      * @param ticketId
229      *            The ticket id associated with this log message.
230      */
231     public void logInfo(final String message, final String ticketId) {
232 
233         if (Level.INFO_INT >= logLevel)
234             getLogger().info(generateLogMessage(message, ticketId, null));
235     }
236 
237 
238     /***
239      * Logs a message with level info including ticket id.
240      * @param message
241      *            The message to log.
242      * @param ticketId
243      *            The ticket id associated with this log message.
244      * @param throwable
245      *            An throwable associated with this log entry. Message and
246      *            stacktrace will be logged.
247      */
248     public void logInfo(final String message,
249                         final String ticketId,
250                         final Throwable throwable) {
251 
252         if (Level.INFO_INT >= logLevel)
253             getLogger().info(generateLogMessage(message, ticketId, throwable));
254     }
255 
256 
257     /***
258      * Logs a message with level debug.
259      * @param message
260      *            The message to log.
261      */
262     public void logDebug(final String message) {
263 
264         if (Level.DEBUG_INT >= logLevel)
265             getLogger().debug(generateLogMessage(message, null, null));
266     }
267 
268 
269     /***
270      * Logs a message with level debug.
271      * @param message
272      *            The message to log.
273      * @param throwable
274      *            An throwable associated with this log entry. Message and
275      *            stacktrace will be logged.
276      */
277     public void logDebug(final String message, final Throwable throwable) {
278 
279         if (Level.DEBUG_INT >= logLevel)
280             getLogger().debug(generateLogMessage(message, null, throwable));
281     }
282 
283 
284     /***
285      * Logs a message with level debug including ticket id.
286      * @param message
287      *            The message to log.
288      * @param ticketId
289      *            The ticket id associated with this log message.
290      */
291     public void logDebug(final String message, final String ticketId) {
292 
293         if (Level.DEBUG_INT >= logLevel)
294             getLogger().debug(generateLogMessage(message, ticketId, null));
295     }
296 
297 
298     /***
299      * Logs a message with level debug including ticket id.
300      * @param message
301      *            The message to log.
302      * @param ticketId
303      *            The ticket id associated with this log message.
304      * @param throwable
305      *            An throwable associated with this log entry. Message and
306      *            stacktrace will be logged.
307      */
308     public void logDebug(final String message,
309                          final String ticketId,
310                          final Throwable throwable) {
311 
312         if (Level.DEBUG_INT >= logLevel)
313             getLogger().debug(generateLogMessage(message, ticketId, throwable));
314     }
315 
316 
317     /***
318      * Generates the final log entry to give to the underlying log api.
319      * @param message
320      *            The message to log.
321      * @param ticketId
322      *            A ticket id. May be null.
323      * @param throwable
324      *            Throwable to get stacktrace from. May be null.
325      * @return The final log string.
326      */
327     private String generateLogMessage(final String message,
328                                       final String ticketId,
329                                       final Throwable throwable) {
330 
331         StringBuffer buffer = new StringBuffer();
332 
333         /* Add default value '-' if variabel is null */
334         buffer.append(ticketId != null ? "[" + ticketId + "] " : "[-] ");
335         buffer.append(message != null ? "\"" + message + "\"" : "\"-\"");
336 
337         if (throwable != null) {
338             /* Capture stacktrace */
339             OutputStream outputStream = new ByteArrayOutputStream();
340             PrintStream printStream = new PrintStream(outputStream);
341 
342             throwable.printStackTrace(printStream);
343             printStream.flush();
344 
345             buffer.append(System.getProperty("line.separator") + outputStream.toString());
346         }
347         return buffer.toString();
348     }
349 
350 
351     /***
352      * Returns the logger, instantiates it if not already done. Private, so that
353      * nothing is able to override the formatting that is done by
354      * generateLogMessage.
355      * @return The logger instance of this class.
356      */
357     private Logger getLogger() {
358 
359         if (logger == null) {
360             logger = Logger.getLogger(callingClass);
361 
362             /* Cache the log level for the logger. */
363             Level level = logger.getEffectiveLevel();
364 
365             if (level != null) {
366                 logLevel = level.toInt();
367             } else {
368                 logLevel = Level.ALL_INT;
369             }
370         }
371         return logger;
372     }
373 
374 
375     /***
376      * Checks whether the actual log level would generate log output for a given
377      * log level.
378      * @param level
379      *            The desired level to check against.
380      * @return <code>true</code> if log output for the given level would
381      *         result, otherwise <code>false</code>.
382      */
383     public boolean isEnabledFor(final Priority level) {
384         
385         if (logger == null)
386             return false;
387         return logger.isEnabledFor(level);
388 
389     }
390 
391 }