View Javadoc

1   package org.lcsim.util.log;
2   
3   import java.io.PrintWriter;
4   import java.io.StringWriter;
5   import java.util.logging.Formatter;
6   import java.util.logging.LogRecord;
7   
8   /**
9    * A log <code>Formatter</code> that prints only the message and error tracebacks.
10   *
11   * @author Jeremy McCormick <jeremym@slac.stanford.edu>
12   */
13  public class MessageOnlyLogFormatter extends Formatter {
14  
15      /**
16       * Class constructor.
17       */
18      public MessageOnlyLogFormatter() {
19      }
20  
21      /**
22       * Format the <code>LogRecord</code> for printing
23       *
24       * @param record the <code>LogRecord</code> to format
25       */
26      @Override
27      public String format(final LogRecord record) {
28          final StringBuilder sb = new StringBuilder();
29  
30          // Append the message.
31          sb.append(formatMessage(record) + '\n');
32  
33          // Append a traceback if there was an error thrown.
34          if (record.getThrown() != null) {
35              try {
36                  final StringWriter sw = new StringWriter();
37                  final PrintWriter pw = new PrintWriter(sw);
38                  record.getThrown().printStackTrace(pw);
39                  pw.close();
40                  sb.append(sw.toString());
41              } catch (final Exception ex) {
42              }
43          }
44          return sb.toString();
45      }
46  }