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 LogfileFormatter extends java.util.logging.Formatter { 37 38 public final static String COMPLEX_FORMAT_STRING = 39 "yyyy-MM-dd HH:mm:ss,SSSS"; 40 41 private SimpleDateFormat dateFormatter; 42 43 44 public LogfileFormatter(String dateFormatPattern) { 45 dateFormatter = new SimpleDateFormat (dateFormatPattern); 46 } 47 48 public LogfileFormatter() { 49 this(COMPLEX_FORMAT_STRING); 50 } 51 52 53 64 public String format(LogRecord record) { 65 boolean highlight = record.getLevel().intValue() > Level.INFO.intValue(); 67 68 StringBuffer message = new StringBuffer (); 69 Date logDate = new Date (record.getMillis()); 70 message.append(record.getSequenceNumber()); 71 message.append(" - "); 72 message.append(dateFormatter.format(logDate)); 73 message.append(", "); 74 message.append(record.getLevel()); 75 message.append(" [Thread: "); 76 message.append(Thread.currentThread()); 77 message.append("] "); 78 message.append(record.getLoggerName()); 79 message.append(" - "); 80 message.append(record.getSourceMethodName()); 81 message.append(":\n"); 82 message.append(formatMessage(record)); 83 message.append("\n"); 84 if (record.getThrown() != null) { 85 try { 86 StringWriter sw = new StringWriter (); 87 PrintWriter pw = new PrintWriter (sw); 88 record.getThrown().printStackTrace(pw); 89 pw.close(); 90 message.append(sw.toString()); 91 } catch (Exception ex) { 92 } 93 } 94 if (highlight) message.append("\n"); 95 return message.toString(); 96 97 } 98 } 99 | Popular Tags |