001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.rm.logging;
020:
021: import java.util.logging.*;
022: import java.util.*;
023:
024: /**
025: *
026: * Extension of XMLFormatter so that the message tag doesn't escape the
027: * contents of the message string (which are passed in as xml tags).
028: *
029: * @author Michael Bell
030: * @version $Revision: 1.1 $
031: *
032: */
033: public class HarmoniseFormatter extends XMLFormatter {
034:
035: /**
036: * Constructs a new instance of <code>HarmoniseFormatter</code>.
037: */
038: public HarmoniseFormatter() {
039: super ();
040: }
041:
042: /**
043: * Append to the given <code>StringBuffer</code> an escaped version of
044: * the given text string where a <code>null</code> string
045: * is replaced by "<null>".
046: *
047: * @param sb
048: * @param text
049: */
050: private void escape(StringBuffer sb, String text) {
051: if (text == null) {
052: text = "<null>";
053: }
054: sb.append(text);
055: }
056:
057: /* (non-Javadoc)
058: * @see java.util.logging.Formatter#format(java.util.logging.LogRecord)
059: */
060: public String format(LogRecord record) {
061: StringBuffer sb = new StringBuffer(500);
062: sb.append("<record>\n");
063:
064: sb.append(" <date>");
065: sb.append(new Date(record.getMillis()));
066: sb.append("</date>\n");
067:
068: sb.append(" <millis>");
069: sb.append(record.getMillis());
070: sb.append("</millis>\n");
071:
072: sb.append(" <sequence>");
073: sb.append(record.getSequenceNumber());
074: sb.append("</sequence>\n");
075:
076: String name = record.getLoggerName();
077: if (name != null) {
078: sb.append(" <logger>");
079: escape(sb, name);
080: sb.append("</logger>\n");
081: }
082:
083: sb.append(" <level>");
084: escape(sb, record.getLevel().toString());
085: sb.append("</level>\n");
086:
087: if (record.getSourceClassName() != null) {
088: sb.append(" <class>");
089: escape(sb, record.getSourceClassName());
090: sb.append("</class>\n");
091: }
092:
093: if (record.getSourceMethodName() != null) {
094: sb.append(" <method>");
095: escape(sb, record.getSourceMethodName());
096: sb.append("</method>\n");
097: }
098:
099: 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 message = formatMessage(record);
106: sb.append(message);
107: }
108:
109: sb.append("</record>\n");
110: return sb.toString();
111: }
112: }
|