KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/jdk14/org/apache/slide/util/logger/jdk14/LogfileFormatter.java,v 1.2 2004/07/28 09:38:52 ib Exp $
3  * $Revision: 1.2 $
4  * $Date: 2004/07/28 09:38:52 $
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 LogfileFormatter extends java.util.logging.Formatter JavaDoc {
37    
38     public final static String JavaDoc COMPLEX_FORMAT_STRING =
39         "yyyy-MM-dd HH:mm:ss,SSSS";
40
41    private SimpleDateFormat JavaDoc dateFormatter;
42    
43    /** Creates a new instance of LogfileFormatter */
44    public LogfileFormatter(String JavaDoc dateFormatPattern) {
45       dateFormatter = new SimpleDateFormat JavaDoc(dateFormatPattern);
46    }
47
48    public LogfileFormatter() {
49        this(COMPLEX_FORMAT_STRING);
50    }
51    
52    
53    /**
54     * Format the given log record and return the formatted string.
55     * <p>
56     * The resulting formatted String will normally include a
57     * localized and formated version of the LogRecord's message field.
58     * The Formatter.formatMessage convenience method can (optionally)
59     * be used to localize and format the message field.
60     *
61     * @param record the log record to be formatted.
62     * @return the formatted log record
63     */

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