View Javadoc

1   /*
2    * $Id: LoggerService.java 269 2005-08-10 17:49:22Z josem $
3    *
4    * Tarsis
5    * Copyright (C) 2002 Talika Open Source Group
6    *
7    * This program is free software; you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as published by
9    * the Free Software Foundation; either version 2 of the License, or
10   * (at your option) any later version.
11   *
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with this program; if not, write to the Free Software
19   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20   *
21   */
22  
23  package org.talika.tarsis.log;
24  
25  import java.util.Date;
26  import java.text.DateFormat;
27  
28  import org.talika.tarsis.service.AbstractService;
29  
30  /**
31   * Base implementation of <code>Logger</code> interface.<br>
32   * <br>
33   * Logger plugs in system like a service by extending <code>AbstractService</code>.
34   *
35   * @author  Jose M. Palomar
36   * @version $Revision: 269 $
37   */
38  public abstract class LoggerService extends AbstractService implements Logger {
39  
40      // Constants
41      /**
42       * Level literals.
43       */
44      private static final String[] LEVEL_STRINGS = {
45                              "debug",
46                              "info",
47                              "warning",
48                              "error",
49                              "panic" };
50  
51      // Fields
52      /**
53       * Date format.
54       */
55      private static final DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
56  
57      /**
58       * Logger log level.
59       */
60      private int logLevel;
61  
62      // Constructors
63      /**
64       * Creates a new <code>LoggerService</code>. Default level is <code>WARNING</code>.
65       */
66      public LoggerService() {
67          this(WARNING);
68      }
69  
70      /**
71       * Creates a new <code>LoggerService</code> using given log level.
72       *
73       * @param logLevel int log level.
74       */
75      public LoggerService(int logLevel) {
76          super();
77          this.logLevel = logLevel;
78      }
79  
80      // Methods
81      /**
82       * Returns log level parameter.
83       *
84       * @return int log level parameter.
85       * @see org.talika.tarsis.log.Logger#getLogLevel()
86       */
87      public int getLogLevel() {
88          return logLevel;
89      }
90  
91      /**
92       * Sets log level parameter.
93       *
94       * @param logLevel int log level parameter.
95       */
96      public void setLogLevel(int logLevel) {
97          this.logLevel = logLevel;
98      }
99  
100     /**
101      * Returns <code>true</code> if log level is debug or higher.
102      *
103      * @return boolean <code>true</code> if log level is debug or higher.
104      */
105     public final boolean isDebugEnabled() {
106         return (DEBUG < logLevel);
107     }
108 
109     /**
110      * Returns <code>true</code> if log level is info or higher.
111      *
112      * @return boolean <code>true</code> if log level is info or higher.
113      */
114     public final boolean isInfoEnabled() {
115         return (INFO < logLevel);
116     }
117 
118     /**
119      * Returns <code>true</code> if log level is warning or higher.
120      *
121      * @return boolean <code>true</code> if log level is warning or higher.
122      */
123     public final boolean isWarningEnabled()  {
124         return (WARNING < logLevel);
125     }
126 
127     /**
128      * Returns <code>true</code> if log level is error or higher.
129      *
130      * @return boolean <code>true</code> if log level is error or higher.
131      */
132     public final boolean isErrorEnabled() {
133         return (ERROR < logLevel);
134     }
135 
136     /**
137      * Returns <code>true</code> if log level is panic or higher.
138      *
139      * @return boolean <code>true</code> if log level is panic or higher.
140      */
141     public final boolean isPanicEnabled()  {
142         return (PANIC < logLevel);
143     }
144 
145     /**
146      * Log a message using debug level.
147      *
148      * @param msg String message.
149      * @see org.talika.tarsis.log.Logger#logDebug(String)
150      */
151     public final void logDebug(String msg) {
152         log(DEBUG, msg);
153     }
154 
155     /**
156      * Log a message using information level.
157      *
158      * @param msg String message.
159      * @see org.talika.tarsis.log.Logger#logInfo(String)
160      */
161     public final void logInfo(String msg) {
162         log(INFO, msg);
163     }
164 
165     /**
166      * Log a message using warning level.
167      *
168      * @param msg String message.
169      * @see org.talika.tarsis.log.Logger#logWarning(String)
170      */
171     public final void logWarning(String msg) {
172         log(WARNING, msg);
173     }
174 
175     /**
176      * Log an error message using error level.
177      *
178      * @param msg String message.
179      * @param t Throwable a <code>Throwable</code> object.
180      * @see org.talika.tarsis.log.Logger#logError(String, Throwable)
181      */
182     public final void logError(String msg, Throwable t) {
183         log(ERROR, msg, t);
184     }
185 
186     /**
187      * Log a message using panic level.
188      *
189      * @param msg String message.
190      * @see org.talika.tarsis.log.Logger#logPanic(String)
191      */
192     public final void logPanic(String msg) {
193         log(PANIC, msg);
194     }
195 
196     /**
197      * Log an error message using panic level.
198      *
199      * @param msg String message.
200      * @param t Throwable a <code>Throwable</code> object.
201      * @see org.talika.tarsis.log.Logger#logPanic(String, Throwable)
202      */
203     public final void logPanic(String msg, Throwable t) {
204         log(PANIC, msg, t);
205     }
206 
207     /**
208      * Log a message using given level.
209      *
210      * @param level int log level.
211      * @param msg String message.
212      * @see org.talika.tarsis.log.Logger#log(int, String)
213      */
214     public void log(int level, String msg) {
215 
216         if (level < logLevel) {
217             return;
218         }
219 
220         writeLog(level, formatLog(level, msg, null));
221 
222     }
223 
224     /**
225      * Log an error message using given level.
226      *
227      * @param level int log level.
228      * @param msg String message.
229      * @param t Throwable a <code>Throwable</code> object.
230      * @see org.talika.tarsis.log.Logger#log(int, String, Throwable)
231      */
232     public void log(int level, String msg, Throwable t) {
233 
234         if (level < logLevel) {
235             return;
236         }
237 
238         writeLog(level, formatLog(level, msg, t), t);
239 
240     }
241 
242     /**
243      * Writes a message to log.
244      *
245      * @param level int log level.
246      * @param msg String message.
247      */
248     protected abstract void writeLog(int level, String msg);
249 
250     /**
251      * Writes an error message to log.
252      *
253      * @param level int log level.
254      * @param msg String message.
255      * @param t Throwable a <code>Throwable</code> object.
256      */
257     protected abstract void writeLog(int level, String msg, Throwable t);
258 
259     /**
260      * Returns a formatted log message.
261      *
262      * @param level int log level.
263      * @param msg String message.
264      * @param t Throwable a <code>Throwable</code> object.
265      * @return String formatted log message.
266      */
267     protected final String formatLog(int level, String msg, Throwable t) {
268 
269         StringBuffer logMsg = new StringBuffer();
270         logMsg.append(df.format(new Date()));
271         logMsg.append(" - ");
272         logMsg.append(msg);
273 
274         if (t != null && t.getMessage() != null) {
275             logMsg.append(" - ");
276             logMsg.append(t.getMessage());
277         }
278         logMsg.append('\n');
279 
280         return logMsg.toString();
281     }
282 
283 }