KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > log > JDKFormatter


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id:$
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.log;
27
28 import java.io.PrintWriter JavaDoc;
29 import java.io.StringWriter JavaDoc;
30 import java.text.MessageFormat JavaDoc;
31 import java.util.Date JavaDoc;
32 import java.util.logging.Formatter JavaDoc;
33 import java.util.logging.LogRecord JavaDoc;
34
35 /**
36  * Format a record into a readable message ! not too long.
37  * @author Florent Benoit
38  */

39 public class JDKFormatter extends Formatter JavaDoc {
40
41     /**
42      * Date formatter, use the short for date, medium for the time.
43      */

44     private static final String JavaDoc PATTERN_DATE_TIME = "{0,date,short} {0,time,medium}";
45
46     /**
47      * Line separator.
48      */

49     private static final String JavaDoc LINE_SEPARATOR = System.getProperty("line.separator");
50
51     /**
52      * Date object used to convert the data into printable message.
53      */

54     private Date JavaDoc date = new Date JavaDoc();
55
56     /**
57      * Format the given LogRecord.
58      * @param logRecord the log record to be formatted.
59      * @return a formatted log record
60      */

61     @Override JavaDoc
62     public synchronized String JavaDoc format(final LogRecord JavaDoc logRecord) {
63
64         StringBuilder JavaDoc fullMessage = new StringBuilder JavaDoc();
65         // Append date/time
66
date.setTime(logRecord.getMillis());
67         fullMessage.append(MessageFormat.format(PATTERN_DATE_TIME, date));
68
69         // Level
70
fullMessage.append(" (");
71         fullMessage.append(logRecord.getLevel().getLocalizedName().substring(0, 1));
72         fullMessage.append(") ");
73
74         if (logRecord.getSourceClassName() != null) {
75             // Only Class name
76
String JavaDoc[] splitNames = logRecord.getSourceClassName().split("\\.");
77             fullMessage.append(splitNames[splitNames.length - 1]);
78         } else {
79             fullMessage.append(logRecord.getLoggerName());
80         }
81         if (logRecord.getSourceMethodName() != null) {
82             fullMessage.append(".");
83             fullMessage.append(logRecord.getSourceMethodName());
84         }
85         fullMessage.append(" : ");
86
87         // Append message stored in the log record
88
String JavaDoc userMessage = formatMessage(logRecord);
89         fullMessage.append(userMessage);
90
91         // Line separator
92
fullMessage.append(LINE_SEPARATOR);
93
94         // Exception ?
95
if (logRecord.getThrown() != null) {
96             try {
97                 StringWriter JavaDoc stringWriter = new StringWriter JavaDoc();
98                 PrintWriter JavaDoc printWriter = new PrintWriter JavaDoc(stringWriter);
99                 logRecord.getThrown().printStackTrace(printWriter);
100                 printWriter.close();
101                 fullMessage.append(stringWriter.toString());
102             } catch (Exception JavaDoc ex) {
103                 System.err.println("Formatter error" + ex.getMessage());
104             }
105         }
106         return fullMessage.toString();
107     }
108 }
109
Popular Tags