From 1a4091b49a61ffb23e4577c6a5c46d17196b10b1 Mon Sep 17 00:00:00 2001 From: samskivert Date: Fri, 19 Feb 2010 18:56:42 +0000 Subject: [PATCH] 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 --- .../com/samskivert/util/FormatterUtil.java | 53 +++++++++++++++ .../samskivert/util/OneLineLogFormatter.java | 27 +------- .../samskivert/util/TerseLogFormatter.java | 67 +++++++++++++++++++ 3 files changed, 122 insertions(+), 25 deletions(-) create mode 100644 src/java/com/samskivert/util/FormatterUtil.java create mode 100644 src/java/com/samskivert/util/TerseLogFormatter.java diff --git a/src/java/com/samskivert/util/FormatterUtil.java b/src/java/com/samskivert/util/FormatterUtil.java new file mode 100644 index 00000000..61480b55 --- /dev/null +++ b/src/java/com/samskivert/util/FormatterUtil.java @@ -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); + } + } +} diff --git a/src/java/com/samskivert/util/OneLineLogFormatter.java b/src/java/com/samskivert/util/OneLineLogFormatter.java index 7c139ea0..3f29aea3 100644 --- a/src/java/com/samskivert/util/OneLineLogFormatter.java +++ b/src/java/com/samskivert/util/OneLineLogFormatter.java @@ -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) { - } - } } diff --git a/src/java/com/samskivert/util/TerseLogFormatter.java b/src/java/com/samskivert/util/TerseLogFormatter.java new file mode 100644 index 00000000..1d15f916 --- /dev/null +++ b/src/java/com/samskivert/util/TerseLogFormatter.java @@ -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()); + } +}