1 17 18 package org.apache.avalon.logging.logkit; 19 20 import org.apache.avalon.framework.logger.Logger; 21 import org.apache.log.LogEvent; 22 import org.apache.log.format.ExtendedPatternFormatter; 23 import org.apache.log.format.PatternFormatter; 24 import org.apache.log.util.StackIntrospector; 25 26 import org.apache.avalon.util.exception.ExceptionHelper; 27 28 43 public class StandardFormatter 44 extends ExtendedPatternFormatter 45 { 46 private static final int TYPE_CLASS = MAX_TYPE + 1; 47 48 private static final String TYPE_CLASS_STR = "class"; 49 private static final String TYPE_CLASS_SHORT_STR = "short"; 50 51 public static final boolean DEFAULT_STACKTRACE_POLICY = true; 52 53 private final boolean m_stacktrace; 54 55 62 public StandardFormatter( final String pattern ) 63 { 64 this( pattern, DEFAULT_STACKTRACE_POLICY ); 65 } 66 67 75 public StandardFormatter( final String pattern, final boolean trace ) 76 { 77 super( pattern ); 78 m_stacktrace = trace; 79 } 80 81 88 protected String getStackTrace( final Throwable throwable, final String format ) 89 { 90 if( null == throwable ) 91 { 92 return ""; 93 } 94 return ExceptionHelper.packException( throwable, m_stacktrace ); 95 } 96 97 103 protected int getTypeIdFor( final String type ) 104 { 105 if( type.equalsIgnoreCase( TYPE_CLASS_STR ) ) 106 { 107 return TYPE_CLASS; 108 } 109 else 110 { 111 return super.getTypeIdFor( type ); 112 } 113 } 114 115 121 protected String formatPatternRun( LogEvent event, PatternFormatter.PatternRun run ) 122 { 123 switch( run.m_type ) 124 { 125 case TYPE_CLASS: 126 return getClass( run.m_format ); 127 default: 128 return super.formatPatternRun( event, run ); 129 } 130 } 131 132 135 private String getClass( String format ) 136 { 137 final Class clazz = StackIntrospector.getCallerClass( Logger.class ); 138 139 if( null == clazz ) 140 { 141 return "Unknown-class"; 142 } 143 else 144 { 145 String className = clazz.getName(); 147 148 if( TYPE_CLASS_SHORT_STR.equalsIgnoreCase( format ) ) 150 { 151 int pos = className.lastIndexOf( '.' ); 152 153 if( pos >= 0 ) 154 { 155 className = className.substring( pos + 1 ); 156 } 157 } 158 159 return className; 160 } 161 } 162 } 163 | Popular Tags |