1 55 package org.apache.avalon.framework.logger; 56 57 import org.apache.avalon.framework.ExceptionUtil; 58 import org.apache.log.LogEvent; 59 import org.apache.log.format.ExtendedPatternFormatter; 60 import org.apache.log.format.PatternFormatter; 61 import org.apache.log.util.StackIntrospector; 62 63 78 public class AvalonFormatter 79 extends ExtendedPatternFormatter 80 { 81 private static final int TYPE_CLASS = MAX_TYPE + 1; 82 83 private static final String TYPE_CLASS_STR = "class"; 84 private static final String TYPE_CLASS_SHORT_STR = "short"; 85 86 92 public static final int DEFAULT_STACK_DEPTH = 8; 93 94 100 public static final boolean DEFAULT_PRINT_CASCADING = true; 101 102 private final int m_stackDepth; 104 105 private final boolean m_printCascading; 107 108 115 public AvalonFormatter( final String pattern ) 116 { 117 this( pattern, DEFAULT_STACK_DEPTH, DEFAULT_PRINT_CASCADING ); 118 } 119 120 130 public AvalonFormatter( final String pattern, final int stackDepth, 131 final boolean printCascading ) 132 { 133 super( pattern ); 134 m_stackDepth = stackDepth; 135 m_printCascading = printCascading; 136 } 137 138 145 protected String getStackTrace( final Throwable throwable, final String format ) 146 { 147 if( null == throwable ) 148 { 149 return ""; 150 } 151 return ExceptionUtil.printStackTrace( throwable, m_stackDepth, m_printCascading ); 152 } 153 154 160 protected int getTypeIdFor( final String type ) 161 { 162 if( type.equalsIgnoreCase( TYPE_CLASS_STR ) ) 163 { 164 return TYPE_CLASS; 165 } 166 else 167 { 168 return super.getTypeIdFor( type ); 169 } 170 } 171 172 178 protected String formatPatternRun( LogEvent event, PatternFormatter.PatternRun run ) 179 { 180 switch( run.m_type ) 181 { 182 case TYPE_CLASS: 183 return getClass( run.m_format ); 184 default: 185 return super.formatPatternRun( event, run ); 186 } 187 } 188 189 192 private String getClass( String format ) 193 { 194 final Class clazz = StackIntrospector.getCallerClass( Logger.class ); 195 196 if( null == clazz ) 197 { 198 return "Unknown-class"; 199 } 200 else 201 { 202 String className = clazz.getName(); 204 205 if( TYPE_CLASS_SHORT_STR.equalsIgnoreCase( format ) ) 207 { 208 int pos = className.lastIndexOf( '.' ); 209 210 if( pos >= 0 ) 211 { 212 className = className.substring( pos + 1 ); 213 } 214 } 215 216 return className; 217 } 218 } 219 } 220 | Popular Tags |