1 18 19 20 21 package sync4j.framework.logging; 22 23 import java.io.PrintWriter ; 24 import java.io.StringWriter ; 25 import java.text.MessageFormat ; 26 import java.util.Date ; 27 import java.util.logging.Formatter ; 28 import java.util.logging.Level ; 29 import java.util.logging.LogRecord ; 30 31 44 45 public class SimpleFormatter extends Formatter { 46 47 49 private final static String FORMAT = "[{0,date} {0,time}]"; 50 private final static String NL = System.getProperty("line.separator"); 51 52 54 private MessageFormat formatter; 55 56 58 65 public synchronized String format(LogRecord record) { 66 Object args[] = new Object [1]; 67 68 StringBuffer sb = new StringBuffer (); 69 70 Date d = new Date (); 71 72 d.setTime(record.getMillis()); 74 args[0] = d; 75 76 if (formatter == null) { 77 formatter = new MessageFormat (FORMAT); 78 } 79 formatter.format(args, sb, null); 80 if (record.getLevel().intValue() != Level.INFO.intValue()) { 85 sb.append('[').append(record.getLoggerName()).append(']'); 86 } 87 sb.append(' '); 88 sb.append(record.getLevel().getName()); 89 sb.append(": "); 90 sb.append(formatMessage(record)); 91 sb.append(NL); 92 93 if (record.getThrown() != null) { 94 StringWriter sw = new StringWriter ( ); 95 PrintWriter pw = new PrintWriter (sw); 96 try { 97 record.getThrown().printStackTrace(pw); 98 sb.append(sw.toString()); 99 sb.append(NL); 100 } catch (Exception ex) { 101 } finally { 105 try { pw.close(); } catch (Exception e) {} 106 } 107 } 108 109 return sb.toString(); 110 } 111 } 112 | Popular Tags |