SimpleLogFormatter.java

  1. /*
  2.  * SimpleLogFormatter
  3.  *
  4.  * $Id$
  5.  * $HeadURL$
  6.  */
  7. package gov.usgs.util.logging;

  8. import java.util.Date;

  9. import java.util.logging.Formatter;
  10. import java.util.logging.LogRecord;

  11. import java.io.ByteArrayOutputStream;
  12. import java.io.PrintStream;

  13. /**
  14.  * Simple(r) log formatter for java.util.logging messages.
  15.  *
  16.  * Outputs unique dates once, with all messages sharing that time tab indented below.
  17.  *
  18.  * Example Format:
  19.  * <pre>
  20.  * Wed Sep 30 19:31:48 GMT 2009
  21.  * INFO    Exit code=0
  22.  * Wed Sep 30 19:32:52 GMT 2009
  23.  * INFO    [polldir] duplicate product id=urn:earthquake-usgs-gov:shakemap-scraper:global:2009medd:1
  24.  * Wed Sep 30 19:32:53 GMT 2009
  25.  * INFO    [polldir] received urn:earthquake-usgs-gov:shakemap-scraper:global:2009medd:1
  26.  * INFO    [losspager] filtering type 'shakemap-scraper', not allowed
  27.  * INFO    [logging_client] received urn:earthquake-usgs-gov:shakemap-scraper:global:2009medd:1
  28.  * INFO    [shakemap] received urn:earthquake-usgs-gov:shakemap-scraper:global:2009medd:1
  29.  * </pre>
  30.  *
  31.  */
  32. public class SimpleLogFormatter extends Formatter {

  33.     /** Milliseconds in a second. */
  34.     public static final long MILLIS_PER_SECOND = 1000;

  35.     /** When the last LogRecord was processed. */
  36.     private long lastMillis = 0;

  37.     /** Default constructor. */
  38.     public SimpleLogFormatter() {
  39.     }

  40.     /**
  41.      * Format a LogRecord for output.
  42.      *
  43.      * @param record
  44.      *            LogRecord to format.
  45.      * @return formatted LogRecord as String.
  46.      */
  47.     public final String format(final LogRecord record) {
  48.         StringBuffer buf = new StringBuffer();

  49.         if (lastMillis == 0) {
  50.             // first run...
  51.             buf.append("\n###\n");
  52.         }

  53.         // chop to nearest second, not outputting millis...
  54.         long millis = (record.getMillis() / MILLIS_PER_SECOND)
  55.                 * MILLIS_PER_SECOND;
  56.         if (millis != lastMillis) {
  57.             lastMillis = millis;
  58.             // add date
  59.             buf.append(new Date(lastMillis).toString()).append("\n");
  60.         }

  61.         // add log message
  62.         buf.append(record.getLevel().toString());
  63.         buf.append("\tthread=").append(record.getThreadID());
  64.         buf.append("\t").append(formatMessage(record));
  65.         buf.append("\n");

  66.         // output any associated exception
  67.         Throwable thrown = record.getThrown();
  68.         if (thrown != null) {
  69.             ByteArrayOutputStream out = new ByteArrayOutputStream();
  70.             thrown.printStackTrace(new PrintStream(out, true));
  71.             buf.append(new String(out.toByteArray()));
  72.         }

  73.         return buf.toString();
  74.     }

  75. }