/* * Copyright(c)2003 Forward Computing and Control Pty. Ltd. * ACN 003 669 994, NSW, Australia All rights Reserved * * Written by Dr. M.P. Ford * */ package au.com.forward.logging; import java.nio.charset.Charset; import au.com.forward.utils.StringUtils; import java.util.*; import java.util.logging.*; /** * This class provides a robust xml formatter. * *@author Matthew Ford *@created 3 Oct 2003 */ public class LoggingXMLFormatter extends RobustFormatter { /** * Returns the newLine setting for this class for XML use \n for new lines * *@return the string to use for new lines. */ protected String newLineString() { return "\n"; } /** * Return the header string for a set of XML formatted records. * *@param h The target handler. *@return header string */ public String getHead(Handler h) { StringBuffer sb = new StringBuffer(); sb.append("\n"); sb.append("\n"); sb.append("\n"); sb.append("\n"); return sb.toString(); } /** * Return the tail string for a set of XML formatted records. * *@param h The target handler. *@return tail string */ public String getTail(Handler h) { return "\n"; } /** * Format the given message to XML. * *@param record the log record to be formatted. *@return a formatted log record */ public String format(LogRecord record) { StringBuffer sb = new StringBuffer(500); sb.append("\n"); sb.append(" "); appendISO8601(sb, record.getMillis()); sb.append("\n"); sb.append(" "); sb.append(record.getMillis()); sb.append("\n"); sb.append(" "); sb.append(record.getSequenceNumber()); sb.append("\n"); String name = record.getLoggerName(); if (name != null) { sb.append(" "); escape(sb, name); sb.append("\n"); } sb.append(" "); escape(sb, record.getLevel().toString()); sb.append("\n"); if (record.getSourceClassName() != null) { sb.append(" "); escape(sb, record.getSourceClassName()); sb.append("\n"); } if (record.getSourceMethodName() != null) { sb.append(" "); escape(sb, record.getSourceMethodName()); sb.append("\n"); } sb.append(" "); sb.append(record.getThreadID()); sb.append("\n"); String message = ""; if (record.getMessage() != null) { // Format the message string and its accompanying parameters. message = formatMessage(record); } if (record.getThrown() != null) { Throwable th = record.getThrown(); if (message.length() > 0) { message += "\n"; } message = StringUtils.toString(th); } sb.append(" "); escape(sb, message); sb.append(""); sb.append("\n"); sb.append("\n"); return sb.toString(); } /** * Append to the given StringBuffer an escaped version of the given text * string where XML special characters have been escaped. For a null string we * append "" * *@param sb the StringBuffer to append to *@param text the text to be escaped. */ private void escape(StringBuffer sb, String text) { if (text == null) { text = ""; } for (int i = 0; i < text.length(); i++) { char ch = text.charAt(i); if (ch == '<') { sb.append("<"); } else if (ch == '>') { sb.append(">"); } else if (ch == '&') { sb.append("&"); } else { sb.append(ch); } } } /** * Append the time and date in ISO 8601 format java.text.SimpleDateFormat * dateFormat = new java.text.SimpleDateFormat("yyyy-mm-ddTHH:mm:ss"); stops * this class from loading ?? * *@param sb StringBuffer to append to *@param millis the time in milliseconds */ private void appendISO8601(StringBuffer sb, long millis) { GregorianCalendar calendar = new GregorianCalendar(); calendar.setTimeInMillis(millis); sb.append(calendar.get(Calendar.YEAR)); sb.append('-'); a2(sb, calendar.get(Calendar.MONTH) + 1); sb.append('-'); a2(sb, calendar.get(Calendar.DAY_OF_MONTH)); sb.append('T'); a2(sb, calendar.get(Calendar.HOUR_OF_DAY)); sb.append(':'); a2(sb, calendar.get(Calendar.MINUTE)); sb.append(':'); a2(sb, calendar.get(Calendar.SECOND)); } /** * Append a two digit number. * *@param sb output buffer to append to *@param x int to format */ private void a2(StringBuffer sb, int x) { if (x < 10) { sb.append('0'); } sb.append(x); } }