1 23 24 package org.apache.slide.util.logger.jdk14; 25 26 import java.io.PrintWriter ; 27 import java.io.StringWriter ; 28 import java.text.SimpleDateFormat ; 29 import java.util.Date ; 30 import java.util.logging.Level ; 31 import java.util.logging.LogRecord ; 32 33 36 public class SimpleLogfileFormatter extends java.util.logging.Formatter { 37 38 public final static String SIMPLE_FORMAT_STRING = "MM dd HH:mm:ss"; 39 40 private SimpleDateFormat dateFormatter; 41 42 43 public SimpleLogfileFormatter(String dateFormatPattern) { 44 dateFormatter = new SimpleDateFormat (dateFormatPattern); 45 } 46 47 public SimpleLogfileFormatter() { 48 this(SIMPLE_FORMAT_STRING); 49 } 50 51 62 public String format(LogRecord record) { 63 boolean highlight = record.getLevel().intValue() > Level.INFO.intValue(); 65 66 StringBuffer message = new StringBuffer (); 67 Date logDate = new Date (record.getMillis()); 68 if (highlight) message.append("\n"); 69 message.append(record.getSequenceNumber()); 70 message.append(" - "); 71 message.append(dateFormatter.format(logDate)); 72 message.append(", "); 73 message.append(record.getLevel()); 74 message.append(" - "); 75 message.append(record.getLoggerName()); 76 message.append(" - "); 77 message.append(record.getSourceMethodName()); 78 message.append(":\n"); 79 message.append(formatMessage(record)); 80 message.append("\n"); 81 if (record.getThrown() != null) { 82 try { 83 StringWriter sw = new StringWriter (); 84 PrintWriter pw = new PrintWriter (sw); 85 record.getThrown().printStackTrace(pw); 86 pw.close(); 87 message.append(sw.toString()); 88 } catch (Exception ex) { 89 } 90 } 91 if (highlight) message.append("\n"); 92 return message.toString(); 93 } 94 } 95 | Popular Tags |