KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > framework > logging > SimpleFormatter


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
19
20
21 package sync4j.framework.logging;
22
23 import java.io.PrintWriter JavaDoc;
24 import java.io.StringWriter JavaDoc;
25 import java.text.MessageFormat JavaDoc;
26 import java.util.Date JavaDoc;
27 import java.util.logging.Formatter JavaDoc;
28 import java.util.logging.Level JavaDoc;
29 import java.util.logging.LogRecord JavaDoc;
30
31 /**
32  * This is a very basic logger that traces only <i>level, timestamp and message</i>.
33  * The use of this logger has two advantages:
34  * <ul>
35  * <li>it is less verbose (it does not display class and method name)</li>
36  * <li>it is quicker (it does not look into the stack trace in order to
37  * infer class and method name</li>
38  * </ul>
39  *
40  * @author Stefano Fornari @ Funambol
41  *
42  * @version $Id: SimpleFormatter.java,v 1.3 2005/03/02 20:57:37 harrie Exp $
43  */

44
45 public class SimpleFormatter extends Formatter JavaDoc {
46     
47     // --------------------------------------------------------------- Constants
48

49     private final static String JavaDoc FORMAT = "[{0,date} {0,time}]";
50     private final static String JavaDoc NL = System.getProperty("line.separator");
51     
52     // ---------------------------------------------------------- Private fields
53

54     private MessageFormat JavaDoc formatter;
55     
56     // ---------------------------------------------------------- Public methids
57

58     /**
59      * Format the given LogRecord.
60      *
61      * @param record the log record to be formatted.
62      *
63      * @return a formatted log record as String
64      */

65     public synchronized String JavaDoc format(LogRecord JavaDoc record) {
66         Object JavaDoc args[] = new Object JavaDoc[1];
67         
68         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
69         
70         Date JavaDoc d = new Date JavaDoc();
71         
72         // Minimize memory allocations here.
73
d.setTime(record.getMillis());
74         args[0] = d;
75
76         if (formatter == null) {
77             formatter = new MessageFormat JavaDoc(FORMAT);
78         }
79         formatter.format(args, sb, null);
80         //
81
// Show the loggger name only if the level of the record is more
82
// detailed then INFO
83
//
84
if (record.getLevel().intValue() != Level.INFO.intValue()) {
85             sb.append('[').append(record.getLoggerName()).append(']');
86         }
87         sb.append(' ');
88         sb.append(record.getLevel().getName());
89         sb.append(": ");
90         sb.append(formatMessage(record));
91         sb.append(NL);
92         
93         if (record.getThrown() != null) {
94             StringWriter JavaDoc sw = new StringWriter JavaDoc( );
95             PrintWriter JavaDoc pw = new PrintWriter JavaDoc (sw);
96             try {
97                 record.getThrown().printStackTrace(pw);
98                 sb.append(sw.toString());
99                 sb.append(NL);
100             } catch (Exception JavaDoc ex) {
101                 //
102
// What to do here???
103
//
104
} finally {
105                 try { pw.close(); } catch (Exception JavaDoc e) {}
106             }
107         }
108         
109         return sb.toString();
110     }
111 }
112
Popular Tags