KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > util > logging > SimpleFormatter


1 /*
2  * @(#)SimpleFormatter.java 1.14 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8
9 package java.util.logging;
10
11 import java.io.*;
12 import java.text.*;
13 import java.util.Date JavaDoc;
14
15 /**
16  * Print a brief summary of the LogRecord in a human readable
17  * format. The summary will typically be 1 or 2 lines.
18  *
19  * @version 1.14, 12/19/03
20  * @since 1.4
21  */

22
23 public class SimpleFormatter extends Formatter JavaDoc {
24
25     Date JavaDoc dat = new Date JavaDoc();
26     private final static String JavaDoc format = "{0,date} {0,time}";
27     private MessageFormat formatter;
28
29     private Object JavaDoc args[] = new Object JavaDoc[1];
30
31     // Line separator string. This is the value of the line.separator
32
// property at the moment that the SimpleFormatter was created.
33
private String JavaDoc lineSeparator = (String JavaDoc) java.security.AccessController.doPrivileged(
34                new sun.security.action.GetPropertyAction("line.separator"));
35
36     /**
37      * Format the given LogRecord.
38      * @param record the log record to be formatted.
39      * @return a formatted log record
40      */

41     public synchronized String JavaDoc format(LogRecord JavaDoc record) {
42     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
43     // Minimize memory allocations here.
44
dat.setTime(record.getMillis());
45     args[0] = dat;
46     StringBuffer JavaDoc text = new StringBuffer JavaDoc();
47     if (formatter == null) {
48         formatter = new MessageFormat(format);
49     }
50     formatter.format(args, text, null);
51     sb.append(text);
52     sb.append(" ");
53     if (record.getSourceClassName() != null) {
54         sb.append(record.getSourceClassName());
55     } else {
56         sb.append(record.getLoggerName());
57     }
58     if (record.getSourceMethodName() != null) {
59         sb.append(" ");
60         sb.append(record.getSourceMethodName());
61     }
62     sb.append(lineSeparator);
63     String JavaDoc message = formatMessage(record);
64     sb.append(record.getLevel().getLocalizedName());
65     sb.append(": ");
66     sb.append(message);
67     sb.append(lineSeparator);
68     if (record.getThrown() != null) {
69         try {
70             StringWriter sw = new StringWriter();
71             PrintWriter pw = new PrintWriter(sw);
72             record.getThrown().printStackTrace(pw);
73             pw.close();
74         sb.append(sw.toString());
75         } catch (Exception JavaDoc ex) {
76         }
77     }
78     return sb.toString();
79     }
80 }
81
Popular Tags