Added a TerseLogFormatter for those times when you don't want to spend 80% of

your terminal width on incidentals. Factored some shared bits into
FormatterUtil.


git-svn-id: https://samskivert.googlecode.com/svn/trunk@2750 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
samskivert
2010-02-19 18:56:42 +00:00
parent 383aaa58f8
commit 1a4091b49a
3 changed files with 122 additions and 25 deletions
@@ -0,0 +1,53 @@
//
// $Id$
//
// samskivert library - useful routines for java programs
// Copyright (C) 2001-2010 Michael Bayne, et al.
//
// 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
// (at your option) 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
package com.samskivert.util;
import java.util.logging.Formatter;
import java.util.logging.Handler;
import java.util.logging.LogManager;
import java.util.logging.Logger;
/**
* Helper bits for {@link OneLineLogFormatter} and {@link TerseLogFormatter}.
*/
public class FormatterUtil
{
/** The line separator to use between log messages. */
public static String LINE_SEPARATOR = "\n";
static {
try {
LINE_SEPARATOR = System.getProperty("line.separator");
} catch (Exception e) {
}
}
/**
* Configures the default logging handler to use an instance of the specified formatter when
* formatting messages.
*/
public static void configureDefaultHandler (Formatter formatter)
{
Logger logger = LogManager.getLogManager().getLogger("");
for (Handler handler : logger.getHandlers()) {
handler.setFormatter(formatter);
}
}
}
@@ -28,10 +28,7 @@ import java.text.FieldPosition;
import java.text.SimpleDateFormat;
import java.util.logging.Formatter;
import java.util.logging.Handler;
import java.util.logging.LogManager;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
/**
@@ -94,7 +91,7 @@ public class OneLineLogFormatter extends Formatter
// append the message itself
buf.append(formatMessage(record));
buf.append(LINE_SEPARATOR);
buf.append(FormatterUtil.LINE_SEPARATOR);
// if an exception was also provided, append that
if (record.getThrown() != null) {
@@ -127,19 +124,7 @@ public class OneLineLogFormatter extends Formatter
*/
public static void configureDefaultHandler (boolean showWhere)
{
configureDefaultHandler(new OneLineLogFormatter(showWhere));
}
/**
* Configures the default logging handler to use an instance of the specified formatter when
* formatting messages.
*/
public static void configureDefaultHandler (Formatter formatter)
{
Logger logger = LogManager.getLogManager().getLogger("");
for (Handler handler : logger.getHandlers()) {
handler.setFormatter(formatter);
}
FormatterUtil.configureDefaultHandler(new OneLineLogFormatter(showWhere));
}
protected boolean _showWhere;
@@ -148,12 +133,4 @@ public class OneLineLogFormatter extends Formatter
protected FieldPosition _fpos = new FieldPosition(SimpleDateFormat.DATE_FIELD);
protected static final String DATE_FORMAT = "{0,date} {0,time}";
protected static String LINE_SEPARATOR = "\n";
static {
try {
LINE_SEPARATOR = System.getProperty("line.separator");
} catch (Exception e) {
}
}
}
@@ -0,0 +1,67 @@
//
// $Id$
//
// samskivert library - useful routines for java programs
// Copyright (C) 2001-2010 Michael Bayne, et al.
//
// 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
// (at your option) 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
package com.samskivert.util;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.logging.Formatter;
import java.util.logging.LogRecord;
/**
* A very brief formatter that shows only the message and stack trace (if any).
*/
public class TerseLogFormatter extends Formatter
{
@Override
public String format (LogRecord record)
{
StringBuffer buf = new StringBuffer();
// append the message itself
buf.append(formatMessage(record));
buf.append(FormatterUtil.LINE_SEPARATOR);
// if an exception was also provided, append that
if (record.getThrown() != null) {
try {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
record.getThrown().printStackTrace(pw);
pw.close();
buf.append(sw.toString());
} catch (Exception ex) {
buf.append("Format failure:").append(ex);
}
}
return buf.toString();
}
/**
* Configures the default logging handler to use an instance of this formatter when formatting
* messages.
*/
public static void configureDefaultHandler ()
{
FormatterUtil.configureDefaultHandler(new TerseLogFormatter());
}
}