1 8 package org.jivesoftware.util.log.format; 9 10 import org.jivesoftware.util.Log; 11 import org.jivesoftware.util.log.ContextMap; 12 import org.jivesoftware.util.log.LogEvent; 13 import org.jivesoftware.util.log.util.StackIntrospector; 14 15 29 public class ExtendedPatternFormatter extends PatternFormatter { 30 private final static int TYPE_METHOD = MAX_TYPE + 1; 31 private final static int TYPE_THREAD = MAX_TYPE + 2; 32 33 private final static String TYPE_METHOD_STR = "method"; 34 private final static String TYPE_THREAD_STR = "thread"; 35 36 public ExtendedPatternFormatter(final String format) { 37 super(format); 38 } 39 40 46 protected int getTypeIdFor(final String type) { 47 if (type.equalsIgnoreCase(TYPE_METHOD_STR)) 48 return TYPE_METHOD; 49 else if (type.equalsIgnoreCase(TYPE_THREAD_STR)) 50 return TYPE_THREAD; 51 else { 52 return super.getTypeIdFor(type); 53 } 54 } 55 56 62 protected String formatPatternRun(final LogEvent event, final PatternRun run) { 63 switch (run.m_type) { 64 case TYPE_METHOD: 65 return getMethod(event, run.m_format); 66 case TYPE_THREAD: 67 return getThread(event, run.m_format); 68 default: 69 return super.formatPatternRun(event, run); 70 } 71 } 72 73 80 private String getMethod(final LogEvent event, final String format) { 81 final ContextMap map = event.getContextMap(); 82 if (null != map) { 83 final Object object = map.get("method"); 84 if (null != object) { 85 return object.toString(); 86 } 87 } 88 89 final String result = StackIntrospector.getCallerMethod(Log.class); 91 if (null == result) { 92 return "UnknownMethod"; 93 } 94 return result; 95 } 96 97 104 private String getThread(final LogEvent event, final String format) { 105 final ContextMap map = event.getContextMap(); 106 if (null != map) { 107 final Object object = map.get("thread"); 108 if (null != object) { 109 return object.toString(); 110 } 111 } 112 113 return Thread.currentThread().getName(); 114 } 115 } 116 | Popular Tags |