FileDocCategorySizeDatePackage
TopLinkXMLFormatter.javaAPI DocGlassfish v2 API9236Tue May 22 16:54:46 BST 2007oracle.toplink.essentials.logging

TopLinkXMLFormatter

public class TopLinkXMLFormatter extends XMLFormatter

Format a TopLink LogRecord into a standard XML format.

Fields Summary
Constructors Summary
Methods Summary
private voida2(java.lang.StringBuffer sb, int x)

        if (x < 10) {
            sb.append('0");
        }
        sb.append(x);
    
private voidappendISO8601(java.lang.StringBuffer sb, long millis)

        Calendar date = Calendar.getInstance();
        date.setTime(new Date(millis));
        sb.append(date.get(Calendar.YEAR));
        sb.append('-");
        a2(sb, date.get(Calendar.MONTH) + 1);
        sb.append('-");
        a2(sb, date.get(Calendar.DATE));
        sb.append('T");
        a2(sb, date.get(Calendar.HOUR_OF_DAY));
        sb.append(':");
        a2(sb, date.get(Calendar.MINUTE));
        sb.append(':");
        a2(sb, date.get(Calendar.SECOND));
    
private voidescape(java.lang.StringBuffer sb, java.lang.String text)

        if (text == null) {
            text = "<null>";
        }
        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);
            }
        }
    
public java.lang.Stringformat(java.util.logging.LogRecord record0)
Format the given message to XML.

param
record the log record to be formatted.
return
a formatted log record

        if (!(record0 instanceof TopLinkLogRecord)) {
            return super.format(record0);
        } else {
            TopLinkLogRecord record = (TopLinkLogRecord)record0;

            StringBuffer sb = new StringBuffer(500);
            sb.append("<record>\n");

            if (record.shouldPrintDate()) {
                sb.append("  <date>");
                appendISO8601(sb, record.getMillis());
                sb.append("</date>\n");
    
                sb.append("  <millis>");
                sb.append(record.getMillis());
                sb.append("</millis>\n");
            }

            sb.append("  <sequence>");
            sb.append(record.getSequenceNumber());
            sb.append("</sequence>\n");

            String name = record.getLoggerName();
            if (name != null) {
                sb.append("  <logger>");
                escape(sb, name);
                sb.append("</logger>\n");
            }

            sb.append("  <level>");
            escape(sb, record.getLevel().toString());
            sb.append("</level>\n");

            if (record.getSourceClassName() != null) {
                sb.append("  <class>");
                escape(sb, record.getSourceClassName());
                sb.append("</class>\n");
            }

            if (record.getSourceMethodName() != null) {
                sb.append("  <method>");
                escape(sb, record.getSourceMethodName());
                sb.append("</method>\n");
            }

            if (record.getSessionString() != null) {
                sb.append("  <session>");
                sb.append(record.getSessionString());
                sb.append("</session>\n");
            }

            if (record.getConnection() != null) {
                sb.append("  <connection>");
                sb.append(String.valueOf(System.identityHashCode(record.getConnection())));
                sb.append("</connection>\n");
            }

            if (record.shouldPrintThread()) {
                sb.append("  <thread>");
                sb.append(record.getThreadID());
                sb.append("</thread>\n");
            }

            if (record.getMessage() != null) {
                // Format the message string and its accompanying parameters.
                String message = formatMessage(record);
                sb.append("  <message>");
                escape(sb, message);
                sb.append("</message>");
                sb.append("\n");
            }

            // If the message is being localized, output the key, resource
            // bundle name, and params.
            ResourceBundle bundle = record.getResourceBundle();
            try {
                if ((bundle != null) && (bundle.getString(record.getMessage()) != null)) {
                    sb.append("  <key>");
                    escape(sb, record.getMessage());
                    sb.append("</key>\n");
                    sb.append("  <catalog>");
                    escape(sb, record.getResourceBundleName());
                    sb.append("</catalog>\n");
                    Object[] parameters = record.getParameters();
                    for (int i = 0; i < parameters.length; i++) {
                        sb.append("  <param>");
                        try {
                            escape(sb, parameters[i].toString());
                        } catch (Exception ex) {
                            sb.append("???");
                        }
                        sb.append("</param>\n");
                    }
                }
            } catch (Exception ex) {
                // The message is not in the catalog.  Drop through.
            }

            if (record.getThrown() != null) {
                // Report on the state of the throwable.
                Throwable th = record.getThrown();
                sb.append("  <exception>\n");
                sb.append("    <message>");
                escape(sb, th.toString());
                sb.append("</message>\n");

                if ((record.getLevel().intValue() == Level.SEVERE.intValue()) || 
                        ((record.getLevel().intValue() <= Level.WARNING.intValue()) && record.shouldLogExceptionStackTrace())) {
                    StackTraceElement[] trace = th.getStackTrace();
                    for (int i = 0; i < trace.length; i++) {
                        StackTraceElement frame = trace[i];
                        sb.append("    <frame>\n");
                        sb.append("      <class>");
                        escape(sb, frame.getClassName());
                        sb.append("</class>\n");
                        sb.append("      <method>");
                        escape(sb, frame.getMethodName());
                        sb.append("</method>\n");
                        // Check for a line number.
                        if (frame.getLineNumber() >= 0) {
                            sb.append("      <line>");
                            sb.append(frame.getLineNumber());
                            sb.append("</line>\n");
                        }
                        sb.append("    </frame>\n");
                    }
                }
                
                sb.append("  </exception>\n");
            }

            sb.append("</record>\n");
            return sb.toString();
        }