1 5 6 package org.joseki.logging.java; 7 8 import java.util.Date ; 9 import java.text.* ; 10 import java.io.* ; 11 import java.util.logging.*; 12 13 18 public class OneLineFormatter extends Formatter 19 { 20 static public int nameColWidth = 25 ; 23 24 Date date = new Date (); 25 26 31 34 public DateFormat dateAndTimeFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss") ; 36 37 private String lineSeparator = 40 (String ) java.security.AccessController.doPrivileged( 41 new sun.security.action.GetPropertyAction("line.separator")); 42 43 48 public synchronized String format(LogRecord record) 49 { 50 StringBuffer sb = new StringBuffer (); 51 date.setTime(record.getMillis()); 53 StringBuffer text = new StringBuffer (); 54 55 sb.append(dateAndTimeFormat.format(date)) ; 56 57 sb.append(" ") ; 58 String tmp = record.getLevel().getLocalizedName() ; 59 sb.append(tmp); 60 for ( int i = tmp.length() ; i < 6 ; i++ ) 62 sb.append(' ') ; 63 64 sb.append(" : "); 65 66 int len1 = sb.length() ; 67 68 72 if (record.getSourceClassName() != null) 73 { 74 if (!record.getSourceClassName().equals("")) 75 { 76 sb.append(" "); 77 String str = record.getSourceClassName(); 78 int i = str.lastIndexOf('.'); 79 if (i >= 0) 80 sb.append(str.substring(i + 1)); 81 else 82 sb.append(str) ; 83 } 84 } 85 86 92 for ( int i = sb.length() ; i < len1+nameColWidth ; i++ ) 94 sb.append(' ') ; 95 96 sb.append(" :: ") ; 99 100 if ( record.getLevel().equals(Level.WARNING) || 101 record.getLevel().equals(Level.SEVERE) ) 102 { 103 sb.append(record.getLevel().getLocalizedName()) ; 104 sb.append(" :: ") ; 105 } 106 107 108 String message = formatMessage(record); 109 sb.append(message); 110 111 sb.append(lineSeparator); 112 if (record.getThrown() != null) 113 { 114 try 115 { 116 StringWriter sw = new StringWriter(); 117 PrintWriter pw = new PrintWriter(sw); 118 record.getThrown().printStackTrace(pw); 119 pw.close(); 120 sb.append(sw.toString()); 121 } 122 catch (Exception ex) 123 { 124 } 125 } 126 return sb.toString(); 127 } 128 } 129 130 156 157 | Popular Tags |