1 46 47 package org.mr.core.log; 48 49 import java.text.DateFormat ; 50 import java.text.SimpleDateFormat ; 51 import java.util.Date ; 52 import java.util.StringTokenizer ; 53 54 import org.apache.log4j.Layout; 55 import org.apache.log4j.spi.LoggingEvent; 56 57 import org.mr.core.util.SystemTime; 58 59 60 61 62 public class LogLayout extends Layout { 63 64 66 public String format(LoggingEvent logEvent) { 67 StringBuffer sb = new StringBuffer (); 68 sb.append(dateFormat.format(new Date (SystemTime.currentTimeMillis()))); 69 sb.append(" ["); 70 sb.append(logEvent.getThreadName()); 71 sb.append("] "); 72 73 String [] decorator = getDecoratorForLevel(logEvent.getLevel()); 74 sb.append(decorator[0]); 75 sb.append(logEvent.getLevel()); 76 sb.append(decorator[1]); 77 sb.append(" "); 78 sb.append(logEvent.getLoggerName()); 79 sb.append(MESSAGE_DELIMS); 80 Object messageObj = logEvent.getMessage(); 81 String message = (messageObj == null) ? "" : messageObj.toString(); 82 91 sb.append(message); 93 if(useSeparator()) 95 sb.append(getLogSeparator()); 96 return sb.toString(); 97 } 98 99 public boolean useSeparator() { 100 return useSeparator; 101 } 102 103 public void setUseSeparator(boolean useSeparator) { 104 this.useSeparator = useSeparator; 105 } 106 107 public String getLogSeparator() { 108 return logSeparator; 109 } 110 111 112 public void setDecorators(String decorators){ 113 StringTokenizer st = new StringTokenizer (decorators, "|"); 114 while(st.hasMoreTokens()){ 115 try { 116 String tok = st.nextToken(); 117 int ind = tok.indexOf(':'); 118 String type = tok.substring(0, ind); 119 String dec = tok.substring(ind+1); 120 int half = dec.length()/2; 121 String [] both = new String [2]; 122 both[0] = dec.substring(0,half); 123 both[1] = dec.substring(half); 124 if("debug".equalsIgnoreCase(type)){ 125 debugDecorators = both; 126 } 127 if("info".equalsIgnoreCase(type)){ 128 infoDecorators = both; 129 } 130 if("warn".equalsIgnoreCase(type)){ 131 warnDecorators = both; 132 } 133 if("error".equalsIgnoreCase(type)){ 134 errorDecorators = both; 135 } 136 if("fatal".equalsIgnoreCase(type)){ 137 fatalDecorators = both; 138 } 139 } catch (Exception e) { 140 } 142 } 143 } 144 145 146 public boolean ignoresThrowable() { 147 return true; 148 } 149 150 public void activateOptions() { 151 } 152 153 private String [] getDecoratorForLevel(org.apache.log4j.Level level){ 154 switch (level.toInt()) { 155 case 50000 : return fatalDecorators; 156 case 40000 : return errorDecorators; 157 case 30000 : return warnDecorators; 158 case 20000 : return infoDecorators; 159 case 10000 : return debugDecorators; 160 default :return EMPTY_DECORATOR; 161 } 162 } 163 175 176 178 protected boolean useSeparator = true; 179 protected boolean useDecorators = false; 180 protected String logSeparator = "\r\n___________________________________________________________________________________\r\n"; 181 182 protected String [] fatalDecorators = EMPTY_DECORATOR; 183 protected String [] errorDecorators = EMPTY_DECORATOR; 184 protected String [] warnDecorators = EMPTY_DECORATOR; 185 protected String [] infoDecorators = EMPTY_DECORATOR; 186 protected String [] debugDecorators = EMPTY_DECORATOR; 187 188 private static final String [] EMPTY_DECORATOR = {"", ""}; 189 190 192 protected static DateFormat dateFormat = new SimpleDateFormat ("MM/dd/yy h:mm:ss a"); 193 194 195 protected static String MESSAGE_DELIMS = " : "; 198 199 } 200 | Popular Tags |