KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > syncclient > common > logging > Logger


1 /**
2  * Copyright (C) 2003-2005 Funambol
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package sync4j.syncclient.common.logging;
19
20 import java.util.Calendar JavaDoc;
21 import java.util.Date JavaDoc;
22
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25
26 import sync4j.syncclient.common.FileSystemTools;
27
28 /**
29  * This class implement log methods
30  *
31  * @author Fabio Maggi @ Funambol
32  * @version $Id: Logger.java,v 1.9 2005/05/20 07:40:33 luigiafassina Exp $
33  *
34  **/

35 public class Logger {
36
37     //-------------------------------------------------------------- Constants
38

39     public static final int NONE = 0 ;
40     public static final int ERROR = 1 ;
41     public static final int INFO = 5 ;
42     public static final int DEBUG = 10 ;
43
44     public static final String JavaDoc DEFAULT_LOG_FILE_NAME = "sync.log";
45
46     public static final String JavaDoc PROP_NONE = "none" ;
47     public static final String JavaDoc PROP_INFO = "info" ;
48     public static final String JavaDoc PROP_DEBUG = "debug" ;
49
50     private static final int DEFAULT_LOGGER_LEVEL = INFO ;
51     private static final boolean DEFAULT_CONSOLE_ENABLE = true ;
52
53     private static final String JavaDoc MSG_LOG_START = "# SyncClient API J2SE Log";
54
55     private static boolean fileInitialized = false;
56     private static boolean consoleHandlerInitialized = false;
57     private static boolean handlerInitialized = false;
58
59     //------------------------------------------------------------ Private data
60
private static Handler handler = null;
61     private static Handler consoleHandler = new OutputHandler();
62
63     private static int level = 0 ;
64     private static boolean consoleEnable = false;
65     private static File JavaDoc logFile = null ;
66
67     //------------------------------------------------------------ Public methods
68

69     /**
70      * set handler
71      *
72      * @param h the client handler
73      * (default sync4j.syncclient.common.logging.OutputHandler)
74      **/

75     public static void setHandler(Handler h) {
76         if (h != handler) {
77             // force handler init
78
handlerInitialized = false;
79         }
80         handler = h;
81     }
82
83     /**
84      * return the client handler
85      **/

86     public static Handler getHandler() {
87         return handler;
88     }
89
90     /**
91      * set logger level
92      *
93      * @param l the logger level
94      **/

95     public static void setLevel(int l) {
96         level = l;
97     }
98
99     /**
100      * set logger level
101      *
102      * @param l the logger level
103      **/

104     public static void setLevel(String JavaDoc l) {
105
106         if (PROP_NONE.equals(l)) {
107             level = NONE;
108         } else if (PROP_INFO.equals(l)) {
109             level = INFO;
110         } else if (PROP_DEBUG.equals(l)) {
111             level = DEBUG;
112         }
113     }
114
115     /**
116      * set default logger level
117      **/

118     public static void setDefaultLevel() {
119         level = DEFAULT_LOGGER_LEVEL;
120     }
121
122     /**
123      * set log file
124      *
125      * @param fileName
126      **/

127     public static void setLogFile(String JavaDoc fileName) {
128         logFile = new File JavaDoc(fileName);
129     }
130
131     /**
132      * set default log file name
133      **/

134     public static void setDefaultLogFile() {
135         logFile = new File JavaDoc(DEFAULT_LOG_FILE_NAME);
136     }
137
138     /**
139      * set enable / disable log console
140      *
141      * @param enable
142      **/

143     public static void setEnableConsole(boolean enable) {
144         consoleEnable = enable;
145     }
146
147     /**
148      * set default enable console
149      **/

150     public static void setDefaultEnableConsole() {
151         consoleEnable = DEFAULT_CONSOLE_ENABLE;
152     }
153
154     /**
155      * check log level
156      *
157      * @param l the log level to check
158      *
159      * @return <p>true</p> if level is setting
160      **/

161     public static boolean isLoggable(int l) {
162         if (level >= l) {
163             return true;
164         } else {
165             return false;
166         }
167     }
168
169     /**
170      * Logs a string at info level
171      *
172      * @param text the string to be logged
173      **/

174     public static void info(String JavaDoc text) {
175         if (!isLoggable(INFO)) {
176             return;
177         }
178         composeMessage("INFO", text);
179     }
180
181     /**
182      * Logs a string at debug level.
183      *
184      * @param text the string to be logged
185      **/

186     public static void debug(String JavaDoc text) {
187         if (!isLoggable(DEBUG)) {
188             return;
189         }
190         composeMessage("DEBUG", text);
191     }
192
193     /**
194      * Logs a string only if level is info or debug: this is used for the
195      * exception or error messages.
196      *
197      * @param text the string to be logged
198      **/

199     public static void error(String JavaDoc text) {
200         if (!isLoggable(ERROR)) {
201             return;
202         }
203         composeMessage("ERROR", text);
204     }
205
206     //---------------------------------------------------------- Private methods
207

208     /**
209      * Write and Print logger message
210      *
211      * @param level could be INFO, DEBUG or ERROR
212      * @param text the text of the message
213      */

214     private static void composeMessage(String JavaDoc level, String JavaDoc text) {
215         checkInit();
216
217         try {
218
219             text = getDateTime(true)
220                  + " [" + level + "] - "
221                  + text
222                  + "\n"
223                  ;
224
225             FileSystemTools.writeTextFile(logFile, text, true);
226             if (consoleEnable) {
227                 consoleHandler.printMessage(text);
228             }
229
230             if (handler != null) {
231                 handler.printMessage(text);
232             }
233
234         } catch (Exception JavaDoc e) {
235             e.printStackTrace();
236         }
237     }
238
239     /**
240      * Initialize log file, consoleHandler and handler if necessary
241      */

242     private static void checkInit() {
243         if (!fileInitialized) {
244           initLogFile();
245         }
246
247         if (!consoleHandlerInitialized) {
248             initConsoleHandler();
249         }
250
251         if (handler != null && !handlerInitialized) {
252             initHandler();
253         }
254     }
255
256     /**
257      * Init log file
258      **/

259     private static synchronized void initLogFile() {
260
261         if (fileInitialized) {
262             return ;
263         }
264
265         try {
266
267             String JavaDoc initMsg = null;
268
269             initMsg = getDateTime(false) +
270                       " - " +
271                       MSG_LOG_START +
272                       "\n" ;
273
274             FileSystemTools.writeTextFile(logFile, initMsg, false);
275
276             fileInitialized = true;
277
278         } catch (Exception JavaDoc e) {
279             System.err.println("Unable to initialize the logger [" + e.getMessage() + "]");
280         }
281
282     }
283
284     /**
285      * Init handler
286      **/

287     private static synchronized void initHandler() {
288
289         if (handlerInitialized) {
290             return ;
291         }
292
293         try {
294
295             String JavaDoc initMsg = null;
296
297             initMsg = getDateTime(false) +
298                       " - " +
299                       MSG_LOG_START +
300                       "\n" ;
301
302
303             handler.printMessage(initMsg);
304
305             handlerInitialized = true;
306
307         } catch (Exception JavaDoc e) {
308             System.err.println("Unable to initialize the handler [" + e.getMessage() + "]");
309         }
310
311     }
312
313     /**
314      * Init console handler
315      **/

316     private static synchronized void initConsoleHandler() {
317
318         if (!consoleEnable || consoleHandlerInitialized) {
319             return ;
320         }
321
322         try {
323
324             String JavaDoc initMsg = null;
325
326             initMsg = getDateTime(false) +
327                       " - " +
328                       MSG_LOG_START +
329                       "\n" ;
330
331
332             consoleHandler.printMessage(initMsg);
333
334             consoleHandlerInitialized = true;
335
336         } catch (Exception JavaDoc e) {
337             System.err.println("Unable to initialize the handler [" + e.getMessage() + "]");
338         }
339
340     }
341
342     /**
343      *
344      * Return now in "[YYYY-MM-DD hh:mm:ss:SSS]"
345      * format
346      *
347      **/

348     private static String JavaDoc getDateTime(boolean onlyTime) {
349
350         Calendar JavaDoc date = null ;
351
352         String JavaDoc year = null ;
353         String JavaDoc month = null ;
354         String JavaDoc day = null ;
355         String JavaDoc hour = null ;
356         String JavaDoc minute = null ;
357         String JavaDoc second = null ;
358         String JavaDoc millisecond = null ;
359
360         String JavaDoc time = null ;
361         String JavaDoc dateTime = null ;
362
363         date = Calendar.getInstance();
364
365         date.setTime(new Date JavaDoc(System.currentTimeMillis()));
366
367         year = String.valueOf( date.get(Calendar.YEAR ) ) ;
368         month = String.valueOf( date.get(Calendar.MONTH ) ) ;
369         day = String.valueOf( date.get(Calendar.DAY_OF_MONTH ) ) ;
370         hour = String.valueOf( date.get(Calendar.HOUR_OF_DAY ) ) ;
371         minute = String.valueOf( date.get(Calendar.MINUTE ) ) ;
372         second = String.valueOf( date.get(Calendar.SECOND ) ) ;
373         millisecond = String.valueOf( date.get(Calendar.MILLISECOND ) ) ;
374
375         if (month.length() == 1 ) {
376             month = "0" + month;
377         }
378         if (day.length() == 1 ) {
379             day = "0" + day;
380         }
381         if (hour.length() == 1 ) {
382             hour = "0" + hour;
383         }
384         if (minute.length() == 1 ) {
385             minute = "0" + minute;
386         }
387         if (second.length() == 1 ) {
388             second = "0" + second;
389         }
390         if (millisecond.length() == 1 ) {
391             millisecond = "00" + millisecond;
392         }
393         if (millisecond.length() == 2 ) {
394             millisecond = "0" + millisecond;
395         }
396
397         time = hour +
398                 ":" +
399                 minute +
400                 ":" +
401                 second +
402                 ":" +
403                 millisecond ;
404
405         if (onlyTime) {
406             return time;
407         }
408
409         dateTime = year +
410                     "-" +
411                     month +
412                     "-" +
413                     day +
414                     " " +
415                     time ;
416
417         return dateTime;
418
419     }
420
421 }
422
Popular Tags