KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > logging > logkit > StandardFormatter


1 /*
2  * Copyright 2004 Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

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 /**
29  * This formatter extends ExtendedPatternFormatter so that
30  * CascadingExceptions are formatted with all nested exceptions.
31  *
32  * <ul>
33  * <li><code>class</code> : outputs the name of the class that has logged the
34  * message. The optional <code>short</code> subformat removes the
35  * package name. Warning : this pattern works only if formatting occurs in
36  * the same thread as the call to Logger, i.e. it won't work with
37  * <code>AsyncLogTarget</code>.</li>
38  * </ul>
39  *
40  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
41  * @version CVS $Revision: 1.3 $ $Date: 2004/03/17 10:50:04 $
42  */

43 public class StandardFormatter
44     extends ExtendedPatternFormatter
45 {
46     private static final int TYPE_CLASS = MAX_TYPE + 1;
47
48     private static final String JavaDoc TYPE_CLASS_STR = "class";
49     private static final String JavaDoc TYPE_CLASS_SHORT_STR = "short";
50
51     public static final boolean DEFAULT_STACKTRACE_POLICY = true;
52
53     private final boolean m_stacktrace;
54
55     /**
56      * Construct the formatter with the specified pattern
57      * and which which prints out exceptions to stackDepth of 8.
58      *
59      * @param pattern The pattern to use to format the log entries
60      * @since 1.0
61     */

62     public StandardFormatter( final String JavaDoc pattern )
63     {
64         this( pattern, DEFAULT_STACKTRACE_POLICY );
65     }
66
67     /**
68      * Construct the formatter with the specified pattern
69      * and which which prints out exceptions to stackDepth specified.
70      *
71      * @param pattern The pattern to use to format the log entries
72      * @param trace if TRUE generate a stack trace
73      * @since 1.0
74      */

75     public StandardFormatter( final String JavaDoc pattern, final boolean trace )
76     {
77         super( pattern );
78         m_stacktrace = trace;
79     }
80
81     /**
82      * Utility method to format stack trace.
83      *
84      * @param throwable the throwable instance
85      * @param format ancilliary format parameter - allowed to be null
86      * @return the formatted string
87      */

88     protected String JavaDoc getStackTrace( final Throwable JavaDoc throwable, final String JavaDoc format )
89     {
90         if( null == throwable )
91         {
92             return "";
93         }
94         return ExceptionHelper.packException( throwable, m_stacktrace );
95     }
96
97     /**
98      * Retrieve the type-id for a particular string.
99      *
100      * @param type the string
101      * @return the type-id
102      */

103     protected int getTypeIdFor( final String JavaDoc 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    /**
116     * Return the result of formaltting a pattern run.
117     * @param event the log event
118     * @param run the patter formatter pattern run
119     * @return the formatted string
120     */

121     protected String JavaDoc 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     /**
133      * Finds the class that has called Logger.
134      */

135     private String JavaDoc getClass( String JavaDoc format )
136     {
137         final Class JavaDoc clazz = StackIntrospector.getCallerClass( Logger.class );
138
139         if( null == clazz )
140         {
141             return "Unknown-class";
142         }
143         else
144         {
145             // Found : the caller is the previous stack element
146
String JavaDoc className = clazz.getName();
147
148             // Handle optional format
149
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