1 14 15 package org.quickserver.util.logging; 16 17 import java.util.Date ; 18 import java.text.SimpleDateFormat ; 19 import java.util.logging.*; 20 import java.io.*; 21 import org.quickserver.util.MyString; 22 23 27 public class SimpleTextFormatter extends Formatter { 28 private Date date = new Date (); 29 private SimpleDateFormat df = new SimpleDateFormat ("MM-dd-yyyy hh:mm:ss,SSS"); 30 31 private String lineSeparator = (String ) java.security.AccessController.doPrivileged( 32 new sun.security.action.GetPropertyAction("line.separator")); 33 34 public synchronized String format(LogRecord record) { 35 date.setTime(record.getMillis()); 36 37 StringBuffer sb = new StringBuffer (); 38 sb.append(df.format(date)); 39 sb.append(" ["); 40 sb.append(MyString.alignLeft(record.getLevel().getLocalizedName(), 7)); 41 sb.append("] "); 42 if(record.getSourceClassName() != null) { 43 sb.append(record.getSourceClassName()); 44 } else { 45 sb.append(record.getLoggerName()); 46 } 47 if(record.getSourceMethodName() != null) { 48 sb.append('.'); 49 sb.append(record.getSourceMethodName()); 50 } 51 sb.append(" - "); 52 sb.append(formatMessage(record)); 53 54 if(record.getThrown() != null) { 55 sb.append(lineSeparator); 56 sb.append("[StackTrace: "); 57 try { 58 StringWriter sw = new StringWriter(); 59 PrintWriter pw = new PrintWriter(sw); 60 record.getThrown().printStackTrace(pw); 61 pw.close(); 62 sb.append(sw.toString()); 63 } catch(Exception ex) { 64 sb.append(record.getThrown().toString()); 65 } 66 sb.append(']'); 67 } 68 69 sb.append(lineSeparator); 70 return sb.toString(); 71 } 72 } 73 | Popular Tags |