org.ow2.util.log.JDKFormatter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of util-log Show documentation
Show all versions of util-log Show documentation
Logger that provides i18n and varargs
/**
* EasyBeans
* Copyright (C) 2006 Bull S.A.S.
* Contact: [email protected]
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*
* --------------------------------------------------------------------------
* $Id: JDKFormatter.java 4389 2008-12-15 13:48:57Z alitokmen $
* --------------------------------------------------------------------------
*/
package org.ow2.util.log;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.MessageFormat;
import java.util.Date;
import java.util.logging.Formatter;
import java.util.logging.LogRecord;
/**
* Format a record into a readable message ! not too long.
* @author Florent Benoit
*/
public class JDKFormatter extends Formatter {
/**
* Date formatter, use the short for date, medium for the time.
*/
private static final String PATTERN_DATE_TIME = "{0,date,short} {0,time,medium}";
/**
* Line separator.
*/
private static final String LINE_SEPARATOR = System.getProperty("line.separator");
/**
* Date object used to convert the data into printable message.
*/
private Date date = new Date();
/**
* Format the given LogRecord.
* @param logRecord the log record to be formatted.
* @return a formatted log record
*/
@Override
public synchronized String format(final LogRecord logRecord) {
StringBuilder fullMessage = new StringBuilder();
// Append date/time
date.setTime(logRecord.getMillis());
fullMessage.append(MessageFormat.format(PATTERN_DATE_TIME, date));
// Level
fullMessage.append(" (");
fullMessage.append(logRecord.getLevel().getLocalizedName().substring(0, 1));
fullMessage.append(") ");
if (logRecord.getSourceClassName() != null) {
// Only Class name
String[] splitNames = logRecord.getSourceClassName().split("\\.");
fullMessage.append(splitNames[splitNames.length - 1]);
} else {
fullMessage.append(logRecord.getLoggerName());
}
if (logRecord.getSourceMethodName() != null) {
fullMessage.append(".");
fullMessage.append(logRecord.getSourceMethodName());
}
fullMessage.append(" : ");
// Append message stored in the log record
String userMessage = formatMessage(logRecord);
fullMessage.append(userMessage);
// Line separator
fullMessage.append(LINE_SEPARATOR);
// Exception ?
if (logRecord.getThrown() != null) {
try {
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
logRecord.getThrown().printStackTrace(printWriter);
printWriter.close();
fullMessage.append(stringWriter.toString());
} catch (Exception ex) {
System.err.println("Formatter error" + ex.getMessage());
}
}
return fullMessage.toString();
}
}