KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/jdk14/org/apache/slide/util/logger/jdk14/ConsoleLogfileFormatter.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.util.logging.Level JavaDoc;
29 import java.util.logging.LogRecord JavaDoc;
30
31 /**
32  * Formatter for console output. Tries to present essential information in a manner similar to tomcat.
33  *
34  */

35 public class ConsoleLogfileFormatter extends java.util.logging.Formatter JavaDoc {
36
37     public String JavaDoc format(LogRecord JavaDoc record) {
38         // give it a special appearance if it is a warning or an error
39
boolean highlight = record.getLevel().intValue() > Level.INFO.intValue();
40         StringBuffer JavaDoc message = new StringBuffer JavaDoc();
41         
42         if (highlight) message.append("\n");
43         message.append(record.getSequenceNumber());
44         message.append(" ");
45         message.append(record.getLevel());
46         message.append(" ");
47         if (highlight) message.append("\n");
48         message.append(record.getLoggerName());
49         message.append(" - ");
50         message.append(formatMessage(record));
51         message.append("\n");
52         if (record.getThrown() != null) {
53             try {
54                 StringWriter JavaDoc sw = new StringWriter JavaDoc();
55                 PrintWriter JavaDoc pw = new PrintWriter JavaDoc(sw);
56                 record.getThrown().printStackTrace(pw);
57                 pw.close();
58                 message.append(sw.toString());
59             } catch (Exception JavaDoc ex) {
60             }
61         }
62         if (highlight) message.append("\n");
63
64         return message.toString();
65     }
66 }
67
Popular Tags