KickJava   Java API By Example, From Geeks To Geeks.

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


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, 2005, Oracle. All rights reserved.
22
package oracle.toplink.essentials.logging;
23
24 import java.util.*;
25 import java.util.logging.XMLFormatter JavaDoc;
26 import java.util.logging.LogRecord JavaDoc;
27 import java.util.logging.Level JavaDoc;
28
29 /**
30  * <p>
31  * Format a TopLink LogRecord into a standard XML format.
32  * </p>
33  */

34 public class TopLinkXMLFormatter extends XMLFormatter JavaDoc {
35     // Append a two digit number.
36
private void a2(StringBuffer JavaDoc sb, int x) {
37         if (x < 10) {
38             sb.append('0');
39         }
40         sb.append(x);
41     }
42
43     // Append the time and date in ISO 8601 format
44
private void appendISO8601(StringBuffer JavaDoc sb, long millis) {
45         Calendar date = Calendar.getInstance();
46         date.setTime(new Date(millis));
47         sb.append(date.get(Calendar.YEAR));
48         sb.append('-');
49         a2(sb, date.get(Calendar.MONTH) + 1);
50         sb.append('-');
51         a2(sb, date.get(Calendar.DATE));
52         sb.append('T');
53         a2(sb, date.get(Calendar.HOUR_OF_DAY));
54         sb.append(':');
55         a2(sb, date.get(Calendar.MINUTE));
56         sb.append(':');
57         a2(sb, date.get(Calendar.SECOND));
58     }
59
60     // Append to the given StringBuffer an escaped version of the
61
// given text string where XML special characters have been escaped.
62
// For a null string we appebd "<null>"
63
private void escape(StringBuffer JavaDoc sb, String JavaDoc text) {
64         if (text == null) {
65             text = "<null>";
66         }
67         for (int i = 0; i < text.length(); i++) {
68             char ch = text.charAt(i);
69             if (ch == '<') {
70                 sb.append("&lt;");
71             } else if (ch == '>') {
72                 sb.append("&gt;");
73             } else if (ch == '&') {
74                 sb.append("&amp;");
75             } else {
76                 sb.append(ch);
77             }
78         }
79     }
80
81     /**
82      * Format the given message to XML.
83      * @param record the log record to be formatted.
84      * @return a formatted log record
85      */

86     public String JavaDoc format(LogRecord JavaDoc record0) {
87         if (!(record0 instanceof TopLinkLogRecord)) {
88             return super.format(record0);
89         } else {
90             TopLinkLogRecord record = (TopLinkLogRecord)record0;
91
92             StringBuffer JavaDoc sb = new StringBuffer JavaDoc(500);
93             sb.append("<record>\n");
94
95             if (record.shouldPrintDate()) {
96                 sb.append(" <date>");
97                 appendISO8601(sb, record.getMillis());
98                 sb.append("</date>\n");
99     
100                 sb.append(" <millis>");
101                 sb.append(record.getMillis());
102                 sb.append("</millis>\n");
103             }
104
105             sb.append(" <sequence>");
106             sb.append(record.getSequenceNumber());
107             sb.append("</sequence>\n");
108
109             String JavaDoc name = record.getLoggerName();
110             if (name != null) {
111                 sb.append(" <logger>");
112                 escape(sb, name);
113                 sb.append("</logger>\n");
114             }
115
116             sb.append(" <level>");
117             escape(sb, record.getLevel().toString());
118             sb.append("</level>\n");
119
120             if (record.getSourceClassName() != null) {
121                 sb.append(" <class>");
122                 escape(sb, record.getSourceClassName());
123                 sb.append("</class>\n");
124             }
125
126             if (record.getSourceMethodName() != null) {
127                 sb.append(" <method>");
128                 escape(sb, record.getSourceMethodName());
129                 sb.append("</method>\n");
130             }
131
132             if (record.getSessionString() != null) {
133                 sb.append(" <session>");
134                 sb.append(record.getSessionString());
135                 sb.append("</session>\n");
136             }
137
138             if (record.getConnection() != null) {
139                 sb.append(" <connection>");
140                 sb.append(String.valueOf(System.identityHashCode(record.getConnection())));
141                 sb.append("</connection>\n");
142             }
143
144             if (record.shouldPrintThread()) {
145                 sb.append(" <thread>");
146                 sb.append(record.getThreadID());
147                 sb.append("</thread>\n");
148             }
149
150             if (record.getMessage() != null) {
151                 // Format the message string and its accompanying parameters.
152
String JavaDoc message = formatMessage(record);
153                 sb.append(" <message>");
154                 escape(sb, message);
155                 sb.append("</message>");
156                 sb.append("\n");
157             }
158
159             // If the message is being localized, output the key, resource
160
// bundle name, and params.
161
ResourceBundle bundle = record.getResourceBundle();
162             try {
163                 if ((bundle != null) && (bundle.getString(record.getMessage()) != null)) {
164                     sb.append(" <key>");
165                     escape(sb, record.getMessage());
166                     sb.append("</key>\n");
167                     sb.append(" <catalog>");
168                     escape(sb, record.getResourceBundleName());
169                     sb.append("</catalog>\n");
170                     Object JavaDoc[] parameters = record.getParameters();
171                     for (int i = 0; i < parameters.length; i++) {
172                         sb.append(" <param>");
173                         try {
174                             escape(sb, parameters[i].toString());
175                         } catch (Exception JavaDoc ex) {
176                             sb.append("???");
177                         }
178                         sb.append("</param>\n");
179                     }
180                 }
181             } catch (Exception JavaDoc ex) {
182                 // The message is not in the catalog. Drop through.
183
}
184
185             if (record.getThrown() != null) {
186                 // Report on the state of the throwable.
187
Throwable JavaDoc th = record.getThrown();
188                 sb.append(" <exception>\n");
189                 sb.append(" <message>");
190                 escape(sb, th.toString());
191                 sb.append("</message>\n");
192
193                 if ((record.getLevel().intValue() == Level.SEVERE.intValue()) ||
194                         ((record.getLevel().intValue() <= Level.WARNING.intValue()) && record.shouldLogExceptionStackTrace())) {
195                     StackTraceElement JavaDoc[] trace = th.getStackTrace();
196                     for (int i = 0; i < trace.length; i++) {
197                         StackTraceElement JavaDoc frame = trace[i];
198                         sb.append(" <frame>\n");
199                         sb.append(" <class>");
200                         escape(sb, frame.getClassName());
201                         sb.append("</class>\n");
202                         sb.append(" <method>");
203                         escape(sb, frame.getMethodName());
204                         sb.append("</method>\n");
205                         // Check for a line number.
206
if (frame.getLineNumber() >= 0) {
207                             sb.append(" <line>");
208                             sb.append(frame.getLineNumber());
209                             sb.append("</line>\n");
210                         }
211                         sb.append(" </frame>\n");
212                     }
213                 }
214                 
215                 sb.append(" </exception>\n");
216             }
217
218             sb.append("</record>\n");
219             return sb.toString();
220         }
221     }
222 }
223
Popular Tags