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. import java.time.Instant;
  14. import java.time.temporal.ChronoUnit;

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

  37.   /** Milliseconds in a second. */
  38.   public static final long MILLIS_PER_SECOND = 1000;

  39.   /** When the last LogRecord was processed. */
  40.   private long lastMillis = 0;

  41.   /** Whether timestamps are appended to each log entry */
  42.   private boolean timestampsEnabled = false;

  43.   /** Default constructor. */
  44.   public SimpleLogFormatter() {
  45.   }

  46.   /**
  47.    * Constructor with log timestamp option.
  48.    *
  49.    * @param timestampsEnabled flag indicating if timestamps are included or not
  50.    */
  51.   public SimpleLogFormatter(boolean timestampsEnabled) {
  52.     this.timestampsEnabled = timestampsEnabled;
  53.   }

  54.   /**
  55.    * Format a LogRecord for output.
  56.    *
  57.    * @param record LogRecord to format.
  58.    *
  59.    * @return formatted LogRecord as String.
  60.    */
  61.   public final String format(final LogRecord record) {
  62.     StringBuffer buf = new StringBuffer();

  63.     if (lastMillis == 0) {
  64.       // first run...
  65.       buf.append("\n###\n");
  66.     }

  67.     // chop to nearest second, not outputting millis...
  68.     long millis = (record.getMillis() / MILLIS_PER_SECOND) * MILLIS_PER_SECOND;
  69.     if (millis != lastMillis) {
  70.       lastMillis = millis;
  71.       // add date
  72.       buf.append(new Date(lastMillis).toString()).append("\n");
  73.     }

  74.     // append UTC timestamps to each log entry
  75.     if (this.timestampsEnabled) {
  76.       Instant utcDateTime = Instant.now().truncatedTo(ChronoUnit.MILLIS);
  77.       buf.append(utcDateTime).append(" ");
  78.     }

  79.     // add log message
  80.     // buf.append(millis + " ");
  81.     buf.append(record.getLevel().toString());
  82.     buf.append("\tthread=").append(record.getLongThreadID());
  83.     buf.append("\t").append(formatMessage(record));
  84.     buf.append("\n");

  85.     // output any associated exception
  86.     Throwable thrown = record.getThrown();
  87.     if (thrown != null) {
  88.       ByteArrayOutputStream out = new ByteArrayOutputStream();
  89.       thrown.printStackTrace(new PrintStream(out, true));
  90.       buf.append(new String(out.toByteArray()));
  91.     }

  92.     return buf.toString();
  93.   }

  94.   /**
  95.    * Check if time stamps are currently enabled.
  96.    *
  97.    * @return flag indicating if timestamps are currently enabled
  98.    */
  99.   public Boolean getTimestampsEnabled() {
  100.     return this.timestampsEnabled;
  101.   }

  102.   /**
  103.    * Set if timestamps are currently enabled.
  104.    *
  105.    * @param timestampsEnabled True to enable timestamps, false otherwise.
  106.    */
  107.   public void setTimestampsEnabled(Boolean timestampsEnabled) {
  108.     this.timestampsEnabled = timestampsEnabled;
  109.   }

  110. }