1 7 8 9 package java.util.logging; 10 11 import java.io.*; 12 import java.text.*; 13 import java.util.Date ; 14 15 22 23 public class SimpleFormatter extends Formatter { 24 25 Date dat = new Date (); 26 private final static String format = "{0,date} {0,time}"; 27 private MessageFormat formatter; 28 29 private Object args[] = new Object [1]; 30 31 private String lineSeparator = (String ) java.security.AccessController.doPrivileged( 34 new sun.security.action.GetPropertyAction("line.separator")); 35 36 41 public synchronized String format(LogRecord record) { 42 StringBuffer sb = new StringBuffer (); 43 dat.setTime(record.getMillis()); 45 args[0] = dat; 46 StringBuffer text = new StringBuffer (); 47 if (formatter == null) { 48 formatter = new MessageFormat(format); 49 } 50 formatter.format(args, text, null); 51 sb.append(text); 52 sb.append(" "); 53 if (record.getSourceClassName() != null) { 54 sb.append(record.getSourceClassName()); 55 } else { 56 sb.append(record.getLoggerName()); 57 } 58 if (record.getSourceMethodName() != null) { 59 sb.append(" "); 60 sb.append(record.getSourceMethodName()); 61 } 62 sb.append(lineSeparator); 63 String message = formatMessage(record); 64 sb.append(record.getLevel().getLocalizedName()); 65 sb.append(": "); 66 sb.append(message); 67 sb.append(lineSeparator); 68 if (record.getThrown() != null) { 69 try { 70 StringWriter sw = new StringWriter(); 71 PrintWriter pw = new PrintWriter(sw); 72 record.getThrown().printStackTrace(pw); 73 pw.close(); 74 sb.append(sw.toString()); 75 } catch (Exception ex) { 76 } 77 } 78 return sb.toString(); 79 } 80 } 81 | Popular Tags |