1 16 package org.apache.cocoon.util.log; 17 18 import java.util.Map ; 19 20 import org.apache.avalon.framework.logger.LogKitLogger; 21 import org.apache.cocoon.environment.ObjectModelHelper; 22 import org.apache.cocoon.environment.Request; 23 import org.apache.cocoon.util.location.LocatedException; 24 import org.apache.commons.lang.ClassUtils; 25 import org.apache.commons.lang.exception.ExceptionUtils; 26 import org.apache.commons.lang.time.FastDateFormat; 27 import org.apache.log.ContextMap; 28 import org.apache.log.LogEvent; 29 import org.apache.log.Logger; 30 31 52 public class CocoonLogFormatter extends ExtensiblePatternFormatter 53 { 54 58 public static final int DEFAULT_STACK_DEPTH = 8; 59 60 protected final static int TYPE_CLASS = MAX_TYPE + 1; 61 protected final static int TYPE_URI = MAX_TYPE + 2; 62 protected final static int TYPE_THREAD = MAX_TYPE + 3; 63 protected final static int TYPE_HOST = MAX_TYPE + 4; 64 protected final static int TYPE_QUERY = MAX_TYPE + 5; 65 protected final static int TYPE_ROOTTHROWABLE = MAX_TYPE + 6; 66 67 protected final static String TYPE_CLASS_STR = "class"; 68 protected final static String TYPE_CLASS_SHORT_STR = "short"; 69 70 protected final static String TYPE_URI_STR = "uri"; 71 protected final static String TYPE_THREAD_STR = "thread"; 72 protected final static String TYPE_HOST_STR = "host"; 73 protected final static String TYPE_QUERY_STR = "query"; 74 protected final static String TYPE_ROOTTHROWABLE_STR = "rootThrowable"; 75 76 private static final String DEFAULT_TIME_PATTERN = "(yyyy-MM-dd) HH:mm.ss:SSS"; 77 private static final FastDateFormat dateFormatter = FastDateFormat.getInstance(DEFAULT_TIME_PATTERN); 78 79 84 static class CallStack extends SecurityManager { 85 94 public Class [] get() { 95 return getClassContext(); 96 } 97 } 98 99 103 private final Class logkitClass = LogKitLogger.class; 104 105 109 private final Class loggerClass = Logger.class; 110 111 115 private CallStack callStack; 116 117 120 private final int m_stackDepth; 121 122 123 public CocoonLogFormatter() { 124 this(DEFAULT_STACK_DEPTH); 125 } 126 127 public CocoonLogFormatter(int stackDepth) { 128 try { 129 this.callStack = new CallStack(); 130 } catch (SecurityException e) { 131 } 133 this.m_stackDepth = stackDepth; 134 } 135 136 protected int getTypeIdFor(String type) { 137 if (type.equalsIgnoreCase(TYPE_CLASS_STR)) { 140 return TYPE_CLASS; 141 } else if (type.equalsIgnoreCase(TYPE_URI_STR)) { 142 return TYPE_URI; 143 } else if (type.equalsIgnoreCase(TYPE_THREAD_STR)) { 144 return TYPE_THREAD; 145 } else if (type.equalsIgnoreCase(TYPE_HOST_STR)) { 146 return TYPE_HOST; 147 } else if (type.equalsIgnoreCase(TYPE_QUERY_STR)) { 148 return TYPE_QUERY; 149 } else if (type.equalsIgnoreCase(TYPE_ROOTTHROWABLE_STR)) { 150 return TYPE_ROOTTHROWABLE; 151 } else { 152 return super.getTypeIdFor(type); 153 } 154 } 155 156 protected String formatPatternRun(LogEvent event, PatternRun run) { 157 switch (run.m_type) { 160 case TYPE_CLASS: 161 return getClass(run.m_format); 162 case TYPE_URI: 163 return getURI(event.getContextMap()); 164 case TYPE_THREAD: 165 return getThread(event.getContextMap()); 166 case TYPE_HOST: 167 return getHost(event.getContextMap()); 168 case TYPE_QUERY: 169 return getQueryString(event.getContextMap()); 170 case TYPE_ROOTTHROWABLE: 171 Throwable thr = event.getThrowable(); 172 Throwable root = ExceptionUtils.getRootCause(thr); return getStackTrace(root == null ? thr : root, run.m_format); 174 } 175 return super.formatPatternRun(event, run); 176 } 177 178 181 private String getClass(String format) { 182 if (this.callStack != null) { 183 Class [] stack = this.callStack.get(); 184 185 for (int i = stack.length - 1; i >= 0; i--) { 187 if (this.logkitClass.isAssignableFrom(stack[i]) || 188 this.loggerClass.isAssignableFrom(stack[i])) { 189 String className = stack[i + 1].getName(); 191 if (TYPE_CLASS_SHORT_STR.equalsIgnoreCase(format)) { 193 className = ClassUtils.getShortClassName(className); 194 } 195 return className; 196 } 197 } 198 } 199 200 return "Unknown-Class"; 204 } 205 206 209 private String getURI(ContextMap ctxMap) { 210 if (ctxMap != null) { 212 final Object context = ctxMap.get("objectModel"); 213 if (context != null && context instanceof Map ) { 214 final Request request = ObjectModelHelper.getRequest((Map ) context); 216 if (request != null) { 217 return request.getRequestURI(); 218 } 219 } 220 } 221 222 return "Unknown-URI"; 223 } 224 225 228 private String getQueryString(ContextMap ctxMap) { 229 if (ctxMap != null) { 230 final Object context = ctxMap.get("objectModel"); 231 if (context != null && context instanceof Map ) { 232 final Request request = ObjectModelHelper.getRequest((Map ) context); 234 if (request != null) { 235 final String queryString = request.getQueryString(); 236 if (queryString != null) { 237 return "?" + queryString; 238 } 239 } 240 } 241 } 242 return ""; 243 } 244 245 248 private String getHost(ContextMap ctxMap) { 249 if (ctxMap != null) { 251 final Object context = ctxMap.get("objectModel"); 252 if (context != null && context instanceof Map ) { 253 final Request request = ObjectModelHelper.getRequest((Map ) context); 255 if (request != null) { 256 return request.getHeader("host"); 257 } 258 } 259 } 260 261 return "Unknown-Host"; 262 } 263 264 267 private String getThread(ContextMap ctxMap) { 268 if (ctxMap != null) { 270 final String threadName = (String ) ctxMap.get("threadName"); 271 if (threadName != null) { 272 return threadName; 273 } 274 } 275 276 return "Unknown-Thread"; 277 } 278 279 290 protected String getStackTrace(final Throwable throwable, final String format) { 291 if (throwable != null) { 292 LocatedException.ensureCauseChainIsSet(throwable); 293 return ExceptionUtils.getStackTrace(throwable); 294 } 296 297 return null; 298 } 299 300 307 protected String getTime(final long time, String pattern) { 308 if (pattern == null || DEFAULT_TIME_PATTERN.equals(pattern)) { 309 return dateFormatter.format(time); 310 } 311 return FastDateFormat.getInstance(pattern).format(time); 312 } 313 } 314 | Popular Tags |