KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > util > logger > jdk14 > SimpleLogfileFormatter


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/jdk14/org/apache/slide/util/logger/jdk14/SimpleLogfileFormatter.java,v 1.2 2004/07/28 09:38:51 ib Exp $
3  * $Revision: 1.2 $
4  * $Date: 2004/07/28 09:38:51 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2002 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.slide.util.logger.jdk14;
25
26 import java.io.PrintWriter JavaDoc;
27 import java.io.StringWriter JavaDoc;
28 import java.text.SimpleDateFormat JavaDoc;
29 import java.util.Date JavaDoc;
30 import java.util.logging.Level JavaDoc;
31 import java.util.logging.LogRecord JavaDoc;
32
33 /**
34  *
35  */

36 public class SimpleLogfileFormatter extends java.util.logging.Formatter JavaDoc {
37
38     public final static String JavaDoc SIMPLE_FORMAT_STRING = "MM dd HH:mm:ss";
39
40     private SimpleDateFormat JavaDoc dateFormatter;
41
42     /** Creates a new instance of LogfileFormatter */
43     public SimpleLogfileFormatter(String JavaDoc dateFormatPattern) {
44         dateFormatter = new SimpleDateFormat JavaDoc(dateFormatPattern);
45     }
46
47     public SimpleLogfileFormatter() {
48         this(SIMPLE_FORMAT_STRING);
49     }
50
51     /**
52      * Format the given log record and return the formatted string.
53      * <p>
54      * The resulting formatted String will normally include a
55      * localized and formated version of the LogRecord's message field.
56      * The Formatter.formatMessage convenience method can (optionally)
57      * be used to localize and format the message field.
58      *
59      * @param record the log record to be formatted.
60      * @return the formatted log record
61      */

62     public String JavaDoc format(LogRecord JavaDoc record) {
63         // give it a special appearance if it is a warning or an error
64
boolean highlight = record.getLevel().intValue() > Level.INFO.intValue();
65
66         StringBuffer JavaDoc message = new StringBuffer JavaDoc();
67         Date JavaDoc logDate = new Date JavaDoc(record.getMillis());
68         if (highlight) message.append("\n");
69         message.append(record.getSequenceNumber());
70         message.append(" - ");
71         message.append(dateFormatter.format(logDate));
72         message.append(", ");
73         message.append(record.getLevel());
74         message.append(" - ");
75         message.append(record.getLoggerName());
76         message.append(" - ");
77         message.append(record.getSourceMethodName());
78         message.append(":\n");
79         message.append(formatMessage(record));
80         message.append("\n");
81         if (record.getThrown() != null) {
82             try {
83                 StringWriter JavaDoc sw = new StringWriter JavaDoc();
84                 PrintWriter JavaDoc pw = new PrintWriter JavaDoc(sw);
85                 record.getThrown().printStackTrace(pw);
86                 pw.close();
87                 message.append(sw.toString());
88             } catch (Exception JavaDoc ex) {
89             }
90         }
91         if (highlight) message.append("\n");
92         return message.toString();
93     }
94 }
95
Popular Tags