1 16 package org.apache.cocoon.util.log; 17 18 import org.apache.avalon.framework.CascadingThrowable; 19 import org.apache.cocoon.environment.ObjectModelHelper; 20 import org.apache.cocoon.environment.Request; 21 import org.apache.commons.lang.ClassUtils; 22 import org.apache.commons.lang.SystemUtils; 23 import org.apache.log.ContextMap; 24 import org.apache.log.LogEvent; 25 import org.apache.log.Logger; 26 import org.apache.log.format.Formatter; 27 28 import java.io.StringWriter ; 29 import java.text.SimpleDateFormat ; 30 import java.util.Date ; 31 import java.util.Map ; 32 import java.util.StringTokenizer ; 33 34 67 public class XMLCocoonLogFormatter implements Formatter { 68 69 protected final static String TYPE_CLASS_STR = "class"; 70 protected final static String TYPE_CLASS_SHORT_STR = "short"; 71 72 protected final static int TYPE_REQUEST_URI = 0; 73 protected final static int TYPE_CATEGORY = 1; 74 protected final static int TYPE_MESSAGE = 2; 75 protected final static int TYPE_TIME = 3; 76 protected final static int TYPE_RELATIVE_TIME = 4; 77 protected final static int TYPE_THROWABLE = 5; 78 protected final static int TYPE_PRIORITY = 6; 79 protected final static int TYPE_CLASS = 7; 80 protected final static int TYPE_CLASS_SHORT = 8; 81 protected final static int TYPE_THREAD = 9; 82 protected final static int TYPE_HOST = 10; 83 84 public final static String [] typeStrings = new String [] { 85 "uri", "category", "message", "time", "rtime", "throwable", "priority", "class", "class:short", "thread", "host"}; 97 protected final SimpleDateFormat dateFormatter = new SimpleDateFormat ("(yyyy-MM-dd) HH:mm.ss:SSS"); 98 99 protected int[] types; 100 101 107 public String format( final LogEvent event ) { 108 final StringBuffer sb = new StringBuffer (); 109 sb.append("<log-entry>").append(SystemUtils.LINE_SEPARATOR); 110 final String value = this.getRequestId(event.getContextMap()); 111 if (value != null) { 112 sb.append("<request-id>").append(value).append("</request-id>").append(SystemUtils.LINE_SEPARATOR); 113 } 114 for(int i = 0; i < this.types.length; i++) { 115 switch(this.types[i]) { 116 case TYPE_REQUEST_URI: 117 sb.append("<uri>"); 118 sb.append(this.getURI(event.getContextMap())); 119 sb.append("</uri>").append(SystemUtils.LINE_SEPARATOR); 120 break; 121 case TYPE_CLASS: 122 sb.append("<class>"); 123 sb.append(this.getClass(TYPE_CLASS)); 124 sb.append("</class>").append(SystemUtils.LINE_SEPARATOR); 125 break; 126 case TYPE_CLASS_SHORT: 127 sb.append("<class>"); 128 sb.append(this.getClass(TYPE_CLASS_SHORT)); 129 sb.append("</class>").append(SystemUtils.LINE_SEPARATOR); 130 break; 131 case TYPE_THREAD: 132 sb.append("<thread>"); 133 sb.append(this.getThread(event.getContextMap())); 134 sb.append("</thread>").append(SystemUtils.LINE_SEPARATOR); 135 break; 136 case TYPE_RELATIVE_TIME: 137 sb.append("<relative-time>"); 138 sb.append(event.getRelativeTime()); 139 sb.append("</relative-time>").append(SystemUtils.LINE_SEPARATOR); 140 break; 141 case TYPE_TIME: 142 sb.append("<time>"); 143 sb.append(dateFormatter.format(new Date (event.getTime()))); 144 sb.append("</time>").append(SystemUtils.LINE_SEPARATOR); 145 break; 146 case TYPE_THROWABLE: 147 Throwable throwable = event.getThrowable(); 148 if (throwable != null) { 149 sb.append("<throwable><![CDATA[").append(SystemUtils.LINE_SEPARATOR); 150 while (throwable != null) { 151 final StringWriter sw = new StringWriter (); 152 throwable.printStackTrace( new java.io.PrintWriter ( sw ) ); 153 sb.append(sw.toString()); 154 if (throwable instanceof CascadingThrowable ) { 155 throwable = ((CascadingThrowable)throwable).getCause(); 156 } else { 157 throwable = null; 158 } 159 } 160 sb.append(SystemUtils.LINE_SEPARATOR).append("]]> </throwable>").append(SystemUtils.LINE_SEPARATOR); 161 } 162 break; 163 case TYPE_MESSAGE: 164 sb.append("<message><![CDATA[").append(SystemUtils.LINE_SEPARATOR); 165 sb.append(event.getMessage()); 166 sb.append(SystemUtils.LINE_SEPARATOR).append("]]> </message>").append(SystemUtils.LINE_SEPARATOR); 167 break; 168 case TYPE_CATEGORY: 169 sb.append("<category>"); 170 sb.append(event.getCategory()); 171 sb.append("</category>").append(SystemUtils.LINE_SEPARATOR); 172 break; 173 case TYPE_PRIORITY: 174 sb.append("<priority>"); 175 sb.append(event.getPriority().getName()); 176 sb.append("</priority>").append(SystemUtils.LINE_SEPARATOR); 177 break; 178 case TYPE_HOST: 179 sb.append("<host>"); 180 sb.append(getHost(event.getContextMap())); 181 sb.append("</host>"); 182 break; 183 } 184 } 185 sb.append("</log-entry>"); 186 sb.append(SystemUtils.LINE_SEPARATOR); 187 return sb.toString(); 188 } 189 190 193 private String getURI(ContextMap ctxMap) { 194 String result = "Unknown-URI"; 195 196 if (ctxMap != null) { 198 Object context = ctxMap.get("objectModel"); 199 if (context != null &&context instanceof Map) { 200 Request request = ObjectModelHelper.getRequest((Map)context); 202 if (request != null) { 203 result = request.getRequestURI(); 204 } 205 } 206 } 207 return result; 208 } 209 210 private String getHost(ContextMap ctxMap) { 211 String result = "Unknown-host"; 212 213 if (ctxMap != null) { 214 Object context = ctxMap.get("objectModel"); 215 if (context != null && context instanceof Map) { 216 Request request = ObjectModelHelper.getRequest((Map)context); 218 if (request != null) { 219 result = request.getHeader("host"); 220 } 221 } 222 } 223 return result; 224 } 225 226 229 private String getRequestId(ContextMap ctxMap) { 230 String result = null; 231 232 if (ctxMap != null) { 234 Object context = ctxMap.get("request-id"); 235 if (context != null) { 236 result = context.toString(); 237 } 238 } 239 return result; 240 } 241 242 245 private String getClass(int format) { 246 247 Class [] stack = this.callStack.get(); 248 249 for (int i = stack.length-1; i >= 0; i--) { 251 if (this.loggerClass.isAssignableFrom(stack[i])) { 252 253 String className = stack[i+1].getName(); 255 256 if (format == TYPE_CLASS_SHORT) { 258 className = ClassUtils.getShortClassName(className); 259 } 260 return className; 261 } 262 } 263 264 return "Unknown-class"; 267 } 268 269 272 private String getThread(ContextMap ctxMap) { 273 if (ctxMap != null && ctxMap.get("threadName") != null) { 274 return (String )ctxMap.get("threadName"); 275 } else { 276 return "Unknown-thread"; 277 } 278 } 279 280 286 protected int getTypeIdFor(final String type) { 287 for (int index = 0; index < typeStrings.length; index++) { 288 if (type.equalsIgnoreCase(typeStrings[index])) { 289 return index; 290 } 291 } 292 throw new IllegalArgumentException ( "Unknown Type - " + type ); 293 } 294 295 298 public void setTypes(String [] typeStrings) { 299 if (typeStrings != null) { 300 this.types = new int[typeStrings.length]; 301 for (int i = 0; i < typeStrings.length; i++) { 302 this.types[i] = this.getTypeIdFor(typeStrings[i]); 303 } 304 } else { 305 this.types = new int[0]; 306 } 307 } 308 309 312 public void setTypes(String typeString) { 313 if (typeString == null) { 314 this.types = new int[0]; 315 } else { 316 StringTokenizer st = new StringTokenizer (typeString); 318 this.types = new int[st.countTokens()]; 319 for (int i = 0; i < this.types.length; i++) { 320 this.types[i] = this.getTypeIdFor(st.nextToken()); 321 } 322 } 323 } 324 325 326 private Class loggerClass = Logger.class; 327 private CallStack callStack = new CallStack(); 328 329 334 static public class CallStack extends SecurityManager 335 { 336 345 public Class [] get() { 346 return getClassContext(); 347 } 348 } 349 } 350 | Popular Tags |