KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quickserver > util > logging > SimpleTextFormatter


1 /*
2  * This file is part of the QuickServer library
3  * Copyright (C) 2003-2005 QuickServer.org
4  *
5  * Use, modification, copying and distribution of this software is subject to
6  * the terms and conditions of the GNU Lesser General Public License.
7  * You should have received a copy of the GNU LGP License along with this
8  * library; if not, you can download a copy from <http://www.quickserver.org/>.
9  *
10  * For questions, suggestions, bug-reports, enhancement-requests etc.
11  * visit http://www.quickserver.org
12  *
13  */

14
15 package org.quickserver.util.logging;
16
17 import java.util.Date JavaDoc;
18 import java.text.SimpleDateFormat JavaDoc;
19 import java.util.logging.*;
20 import java.io.*;
21 import org.quickserver.util.MyString;
22
23 /**
24  * Formats the LogRecord as "MM-dd-yyyy hh:mm:ss,SSS [LEVEL] - Class.method() - MESSAGE"
25  * @since 1.3.2
26  */

27 public class SimpleTextFormatter extends Formatter {
28     private Date JavaDoc date = new Date JavaDoc();
29     private SimpleDateFormat JavaDoc df = new SimpleDateFormat JavaDoc("MM-dd-yyyy hh:mm:ss,SSS");
30
31     private String JavaDoc lineSeparator = (String JavaDoc) java.security.AccessController.doPrivileged(
32         new sun.security.action.GetPropertyAction("line.separator"));
33
34     public synchronized String JavaDoc format(LogRecord record) {
35         date.setTime(record.getMillis());
36
37         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
38         sb.append(df.format(date));
39         sb.append(" [");
40         sb.append(MyString.alignLeft(record.getLevel().getLocalizedName(), 7));
41         sb.append("] ");
42         if(record.getSourceClassName() != null) {
43             sb.append(record.getSourceClassName());
44         } else {
45             sb.append(record.getLoggerName());
46         }
47         if(record.getSourceMethodName() != null) {
48             sb.append('.');
49             sb.append(record.getSourceMethodName());
50         }
51         sb.append(" - ");
52         sb.append(formatMessage(record));
53                 
54         if(record.getThrown() != null) {
55             sb.append(lineSeparator);
56             sb.append("[StackTrace: ");
57             try {
58                 StringWriter sw = new StringWriter();
59                 PrintWriter pw = new PrintWriter(sw);
60                 record.getThrown().printStackTrace(pw);
61                 pw.close();
62                 sb.append(sw.toString());
63             } catch(Exception JavaDoc ex) {
64                 sb.append(record.getThrown().toString());
65             }
66             sb.append(']');
67         }
68         
69         sb.append(lineSeparator);
70         return sb.toString();
71     }
72 }
73
Popular Tags