View Javadoc

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