ConsoleLogHandler.java
01 /**
02  * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
03  */
04 package net.sourceforge.pmd.util.log;
05 
06 import java.io.PrintWriter;
07 import java.io.StringWriter;
08 import java.util.logging.Formatter;
09 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 }