KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > logging > TopLinkSimpleFormatter


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2006, Oracle. All rights reserved.
22
package oracle.toplink.essentials.logging;
23
24 import java.util.logging.SimpleFormatter JavaDoc;
25 import java.util.logging.LogRecord JavaDoc;
26 import java.util.logging.Level JavaDoc;
27 import java.io.*;
28 import java.text.*;
29 import java.util.Date JavaDoc;
30 import oracle.toplink.essentials.internal.security.*;
31
32 /**
33  * <p>
34  * Print a brief summary of a TopLink LogRecord in a human readable
35  * format. The summary will typically be 1 or 2 lines.
36  * </p>
37  */

38 public class TopLinkSimpleFormatter extends SimpleFormatter JavaDoc {
39     Date JavaDoc dat = new Date JavaDoc();
40     private final static String JavaDoc format = "{0,date} {0,time}";
41     private MessageFormat formatter;
42     private Object JavaDoc[] args = new Object JavaDoc[1];
43
44     // Line separator string. This is the value of the line.separator
45
// property at the moment that the SimpleFormatter was created.
46
private String JavaDoc lineSeparator = (String JavaDoc)PrivilegedAccessHelper.getLineSeparator();
47
48     /**
49      * Format the given LogRecord.
50      * @param record the log record to be formatted.
51      * @return a formatted log record
52      */

53     public synchronized String JavaDoc format(LogRecord JavaDoc record0) {
54         if (!(record0 instanceof TopLinkLogRecord)) {
55             return super.format(record0);
56         } else {
57             TopLinkLogRecord record = (TopLinkLogRecord)record0;
58
59             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
60
61             if (record.shouldPrintDate()) {
62                 // Minimize memory allocations here.
63
dat.setTime(record.getMillis());
64                 args[0] = dat;
65                 StringBuffer JavaDoc text = new StringBuffer JavaDoc();
66                 if (formatter == null) {
67                     formatter = new MessageFormat(format);
68                 }
69                 formatter.format(args, text, null);
70                 sb.append(text);
71                 sb.append(" ");
72             }
73             if (record.getSourceClassName() != null) {
74                 sb.append(record.getSourceClassName());
75             } else {
76                 sb.append(record.getLoggerName());
77             }
78             if (record.getSourceMethodName() != null) {
79                 sb.append(" ");
80                 sb.append(record.getSourceMethodName());
81             }
82             if (record.getSessionString() != null) {
83                 sb.append(" ");
84                 sb.append(record.getSessionString());
85             }
86             if (record.getConnection() != null) {
87                 sb.append(" ");
88                 sb.append(AbstractSessionLog.CONNECTION_STRING + "(" + String.valueOf(System.identityHashCode(record.getConnection())) + ")");
89             }
90             if (record.shouldPrintThread()) {
91                 sb.append(" ");
92                 sb.append(AbstractSessionLog.THREAD_STRING + "(" + String.valueOf(record.getThreadID()) + ")");
93             }
94             sb.append(lineSeparator);
95             String JavaDoc message = formatMessage(record);
96             sb.append(record.getLevel().getLocalizedName());
97             sb.append(": ");
98             sb.append(message);
99             sb.append(lineSeparator);
100             if (record.getThrown() != null) {
101                 try {
102                     StringWriter sw = new StringWriter();
103                     PrintWriter pw = new PrintWriter(sw);
104                     if (record.getLevel().intValue() == Level.SEVERE.intValue()) {
105                         record.getThrown().printStackTrace(pw);
106                     } else if (record.getLevel().intValue() <= Level.WARNING.intValue()) {
107                         if (record.shouldLogExceptionStackTrace()) {
108                             record.getThrown().printStackTrace(pw);
109                         } else {
110                             pw.write(record.getThrown().toString());
111                             pw.write(lineSeparator);
112                         }
113                     }
114                     pw.close();
115                     sb.append(sw.toString());
116                 } catch (Exception JavaDoc ex) {
117                 }
118             }
119             return sb.toString();
120         }
121     }
122 }
123
Popular Tags