proyal 2002/10/07 09:37:35
Modified: src/java/org/apache/avalon/framework/logger
AvalonFormatter.java
Log:
* Extend ExtendedPatternFormatter
* Add pattern to put classname that logged the message
Revision Changes Path
1.17 +80 -2
jakarta-avalon/src/java/org/apache/avalon/framework/logger/AvalonFormatter.java
Index: AvalonFormatter.java
===================================================================
RCS file:
/home/cvs/jakarta-avalon/src/java/org/apache/avalon/framework/logger/AvalonFormatter.java,v
retrieving revision 1.16
retrieving revision 1.17
diff -u -r1.16 -r1.17
--- AvalonFormatter.java 5 Aug 2002 13:23:57 -0000 1.16
+++ AvalonFormatter.java 7 Oct 2002 16:37:35 -0000 1.17
@@ -8,18 +8,36 @@
package org.apache.avalon.framework.logger;
import org.apache.avalon.framework.ExceptionUtil;
+import org.apache.log.format.ExtendedPatternFormatter;
import org.apache.log.format.PatternFormatter;
+import org.apache.log.LogEvent;
+import org.apache.log.ContextMap;
+import org.apache.log.util.StackIntrospector;
/**
- * This formatter extends PatternFormatter so that
+ * This formatter extends ExtendedPatternFormatter so that
* CascadingExceptions are formatted with all nested exceptions.
*
+ * <ul>
+ * <li><code>class</code> : outputs the name of the class that has logged the
+ * message. The optional <code>short</code> subformat removes the
+ * package name. Warning : this pattern works only if formatting occurs in
+ * the same thread as the call to Logger, i.e. it won't work with
+ * <code>AsyncLogTarget</code>.</li>
+ * </ul>
+ *
+ * @author <a href="mailto:[EMAIL PROTECTED]">Sylvain Wallez</a>
* @author <a href="mailto:peter at apache.org">Peter Donald</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a>
*/
public class AvalonFormatter
- extends PatternFormatter
+ extends ExtendedPatternFormatter
{
+ private static final int TYPE_CLASS = MAX_TYPE + 1;
+
+ private static final String TYPE_CLASS_STR = "class";
+ private final static String TYPE_CLASS_SHORT_STR = "short";
+
/**
* The constant defining the default stack depth when
* none other is specified.
@@ -80,5 +98,65 @@
return "";
}
return ExceptionUtil.printStackTrace( throwable, m_stackDepth,
m_printCascading );
+ }
+
+ /**
+ * Retrieve the type-id for a particular string.
+ *
+ * @param type the string
+ * @return the type-id
+ */
+ protected int getTypeIdFor( final String type )
+ {
+ if( type.equalsIgnoreCase( TYPE_CLASS_STR ) )
+ {
+ return TYPE_CLASS;
+ }
+ else
+ {
+ return super.getTypeIdFor( type );
+ }
+ }
+
+ protected String formatPatternRun( LogEvent event, PatternFormatter.PatternRun
run )
+ {
+ switch( run.m_type )
+ {
+ case TYPE_CLASS:
+ return getClass( run.m_format );
+ default:
+ return super.formatPatternRun( event, run );
+ }
+ }
+
+ /**
+ * Finds the class that has called Logger.
+ */
+ private String getClass( String format )
+ {
+ final Class clazz = StackIntrospector.getCallerClass( Logger.class );
+
+ if( null == clazz )
+ {
+ return "Unknown-class";
+ }
+ else
+ {
+ // Found : the caller is the previous stack element
+ String className = clazz.getName();
+
+ // Handle optional format
+ if( TYPE_CLASS_SHORT_STR.equalsIgnoreCase( format ) )
+ {
+ int pos = className.lastIndexOf( '.' );
+
+ if( pos >= 0 )
+ {
+ className = className.substring( pos + 1 );
+ }
+ }
+
+ return className;
+ }
}
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>