oracle.toplink.essentials.logging.TopLinkXMLFormatter Maven / Gradle / Ivy
/*
* The contents of this file are subject to the terms
* of the Common Development and Distribution License
* (the "License"). You may not use this file except
* in compliance with the License.
*
* You can obtain a copy of the license at
* glassfish/bootstrap/legal/CDDLv1.0.txt or
* https://glassfish.dev.java.net/public/CDDLv1.0.html.
* See the License for the specific language governing
* permissions and limitations under the License.
*
* When distributing Covered Code, include this CDDL
* HEADER in each file and include the License file at
* glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
* add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your
* own identifying information: Portions Copyright [yyyy]
* [name of copyright owner]
*/
// Copyright (c) 1998, 2007, Oracle. All rights reserved.
package oracle.toplink.essentials.logging;
import java.util.*;
import java.util.logging.XMLFormatter;
import java.util.logging.LogRecord;
import java.util.logging.Level;
/**
*
* Format a TopLink LogRecord into a standard XML format.
*
*/
public class TopLinkXMLFormatter extends XMLFormatter {
// Append a two digit number.
private void a2(StringBuffer sb, int x) {
if (x < 10) {
sb.append('0');
}
sb.append(x);
}
// Append the time and date in ISO 8601 format
private void appendISO8601(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));
}
// 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 appebd ""
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);
}
}
}
/**
* Format the given message to XML.
* @param record the log record to be formatted.
* @return a formatted log record
*/
public String format(LogRecord record0) {
if (!(record0 instanceof TopLinkLogRecord)) {
return super.format(record0);
} else {
TopLinkLogRecord record = (TopLinkLogRecord)record0;
StringBuffer sb = new StringBuffer(500);
sb.append("\n");
if (record.shouldPrintDate()) {
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");
}
if (record.getSessionString() != null) {
sb.append(" ");
sb.append(record.getSessionString());
sb.append(" \n");
}
if (record.getConnection() != null) {
sb.append(" ");
sb.append(String.valueOf(System.identityHashCode(record.getConnection())));
sb.append(" \n");
}
if (record.shouldPrintThread()) {
sb.append(" ");
sb.append(record.getThreadID());
sb.append(" \n");
}
if (record.getMessage() != null) {
// Format the message string and its accompanying parameters.
String message = formatMessage(record);
sb.append(" ");
escape(sb, message);
sb.append(" ");
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(" ");
escape(sb, record.getMessage());
sb.append(" \n");
sb.append(" ");
escape(sb, record.getResourceBundleName());
sb.append(" \n");
Object[] parameters = record.getParameters();
for (int i = 0; i < parameters.length; i++) {
sb.append(" ");
try {
escape(sb, parameters[i].toString());
} catch (Exception ex) {
sb.append("???");
}
sb.append("\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(" \n");
sb.append(" ");
escape(sb, th.toString());
sb.append(" \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(" \n");
sb.append(" ");
escape(sb, frame.getClassName());
sb.append(" \n");
sb.append(" ");
escape(sb, frame.getMethodName());
sb.append(" \n");
// Check for a line number.
if (frame.getLineNumber() >= 0) {
sb.append(" ");
sb.append(frame.getLineNumber());
sb.append(" \n");
}
sb.append(" \n");
}
}
sb.append(" \n");
}
sb.append(" \n");
return sb.toString();
}
}
}