KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > rm > logging > HarmoniseFormatter


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.rm.logging;
20
21 import java.util.logging.*;
22 import java.util.*;
23
24 /**
25  *
26  * Extension of XMLFormatter so that the message tag doesn't escape the
27  * contents of the message string (which are passed in as xml tags).
28  *
29  * @author Michael Bell
30  * @version $Revision: 1.1 $
31  *
32  */

33 public class HarmoniseFormatter extends XMLFormatter {
34
35     /**
36      * Constructs a new instance of <code>HarmoniseFormatter</code>.
37      */

38     public HarmoniseFormatter() {
39         super();
40     }
41
42     /**
43      * Append to the given <code>StringBuffer</code> an escaped version of
44      * the given text string where a <code>null</code> string
45      * is replaced by "&lt;null&gt;".
46      *
47      * @param sb
48      * @param text
49      */

50     private void escape(StringBuffer JavaDoc sb, String JavaDoc text) {
51         if (text == null) {
52             text = "<null>";
53         }
54         sb.append(text);
55     }
56
57     /* (non-Javadoc)
58      * @see java.util.logging.Formatter#format(java.util.logging.LogRecord)
59      */

60     public String JavaDoc format(LogRecord record) {
61         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(500);
62         sb.append("<record>\n");
63
64         sb.append(" <date>");
65         sb.append(new Date(record.getMillis()));
66         sb.append("</date>\n");
67
68         sb.append(" <millis>");
69         sb.append(record.getMillis());
70         sb.append("</millis>\n");
71
72         sb.append(" <sequence>");
73         sb.append(record.getSequenceNumber());
74         sb.append("</sequence>\n");
75
76         String JavaDoc name = record.getLoggerName();
77         if (name != null) {
78             sb.append(" <logger>");
79             escape(sb, name);
80             sb.append("</logger>\n");
81         }
82
83         sb.append(" <level>");
84         escape(sb, record.getLevel().toString());
85         sb.append("</level>\n");
86
87         if (record.getSourceClassName() != null) {
88             sb.append(" <class>");
89             escape(sb, record.getSourceClassName());
90             sb.append("</class>\n");
91         }
92
93         if (record.getSourceMethodName() != null) {
94             sb.append(" <method>");
95             escape(sb, record.getSourceMethodName());
96             sb.append("</method>\n");
97         }
98
99         sb.append(" <thread>");
100         sb.append(record.getThreadID());
101         sb.append("</thread>\n");
102
103         if (record.getMessage() != null) {
104             // Format the message string and its accompanying parameters.
105
String JavaDoc message = formatMessage(record);
106             sb.append(message);
107         }
108
109         sb.append("</record>\n");
110         return sb.toString();
111     }
112 }
113
Popular Tags