View Javadoc

    1   /**
    2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
    3    */
    4   package net.sourceforge.pmd.util;
    5
    6   import java.io.PrintWriter;
    7   import java.io.StringWriter;
    8   import java.util.logging.Formatter;
    9   import java.util.logging.Handler;
    10  import java.util.logging.LogRecord;
    11
    12  /**
    13   * Log to the console using a basic formatter.
    14   *
    15   * @author Wouter Zelle
    16   */
    17  public class ConsoleLogHandler extends Handler {
    18
    19      private static final Formatter FORMATTER = new PmdLogFormatter();
    20
    21      public void publish(LogRecord logRecord) {
    22          System.out.println(FORMATTER.format(logRecord));
    23          if (logRecord.getThrown() != null) {
    24              // Use the same channel, to make sure that the stacktrace comes
    25              // after the message on the console (using printStackTrace
    26              // directly messes things up)
    27              StringWriter stringWriter = new StringWriter();
    28              PrintWriter printWriter = new PrintWriter(stringWriter, true);
    29              logRecord.getThrown().printStackTrace(printWriter);
    30              System.out.println(stringWriter.toString());
    31          }
    32      }
    33
    34      public void close() throws SecurityException {
    35          return;
    36      }
    37
    38      public void flush() {
    39          return;
    40      }
    41  }