1 5 6 package org.joseki.logging.log4j; 7 8 import org.apache.log4j.* ; 9 import org.apache.log4j.spi.*; 10 11 import java.util.* ; 12 import java.text.* ; 13 34 public class OneLineLayout extends Layout 35 { 36 Date date = new Date(); 37 public DateFormat dateAndTimeFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss") ; 38 39 private String lineSeparator = 42 (String ) java.security.AccessController.doPrivileged( 43 new sun.security.action.GetPropertyAction("line.separator")); 44 45 46 static public int nameColWidth = 30 ; 47 48 49 public OneLineLayout() 50 { 51 } 52 53 public String format(LoggingEvent ev) 54 { 55 StringBuffer sb = new StringBuffer (); 56 date.setTime(System.currentTimeMillis()); 58 StringBuffer text = new StringBuffer (); 59 60 sb.append(dateAndTimeFormat.format(date)) ; 61 sb.append(" - ") ; 62 63 boolean nameClassMethod = true ; 64 65 if ( nameClassMethod ) 66 { 67 LocationInfo info = ev.getLocationInformation() ; 68 String clsStr = info.getClassName() ; 69 70 int ind = clsStr.lastIndexOf('.') ; 71 if ( ind > -1 ) 72 clsStr = clsStr.substring(ind+1) ; 73 74 String methStr = info.getMethodName() ; 75 int len = 0 ; 76 77 sb.append(clsStr) ; len += clsStr.length() ; 78 sb.append(".") ; len += 1 ; 79 sb.append(methStr) ; len += methStr.length() ; 80 81 for ( int i = len ; i <= nameColWidth ; i++ ) 82 sb.append(' ') ; 83 } 84 else 85 { 86 87 String loggerName = ev.getLoggerName() ; 88 sb.append(loggerName) ; 89 for ( int i = loggerName.length() ; i <= 20 ; i++ ) 90 sb.append(' ') ; 91 } 92 93 sb.append(" :: ") ; 94 if ( ev.getLevel() == Level.WARN ) 95 sb.append("WARN: ") ; 96 else if ( ev.getLevel() == Level.ERROR ) 97 sb.append("ERROR: ") ; 98 else if ( ev.getLevel() == Level.FATAL ) 99 sb.append("FATAL: ") ; 100 sb.append(ev.getMessage().toString()) ; 101 sb.append(lineSeparator) ; 102 return sb.toString() ; 103 } 104 105 public boolean ignoresThrowable() 106 { 107 return true; 108 } 109 110 public void activateOptions() 111 { 112 } 113 114 } 115 116 117 143 | Popular Tags |