KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log > format > ExtendedPatternFormatter


1 /*
2  * Copyright 1999-2004 The 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 package org.apache.log.format;
18
19 import org.apache.log.ContextMap;
20 import org.apache.log.LogEvent;
21 import org.apache.log.Logger;
22 import org.apache.log.util.StackIntrospector;
23
24 /**
25  * Formatter especially designed for debugging applications.
26  *
27  * This formatter extends the standard PatternFormatter to add
28  * two new possible expansions. These expansions are %{method}
29  * and %{thread}. In both cases the context map is first checked
30  * for values with specified key. This is to facilitate passing
31  * information about caller/thread when threads change (as in
32  * AsyncLogTarget). They then attempt to determine appropriate
33  * information dynamically.
34  *
35  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
36  * @author Peter Donald
37  * @version CVS $Revision: 1.16 $ $Date: 2004/02/28 11:31:24 $
38  */

39 public class ExtendedPatternFormatter
40     extends PatternFormatter
41 {
42     private static final int TYPE_METHOD = MAX_TYPE + 1;
43     private static final int TYPE_THREAD = MAX_TYPE + 2;
44
45     private static final String JavaDoc TYPE_METHOD_STR = "method";
46     private static final String JavaDoc TYPE_THREAD_STR = "thread";
47
48     private int m_callStackOffset = 0;
49
50     /**
51      * Creation of a new extended pattern formatter.
52      * @param format the format string
53      */

54     public ExtendedPatternFormatter( final String JavaDoc format )
55     {
56         this( format, 0 );
57     }
58
59     /**
60      * Creation of a new extended pattern formatter.
61      *
62      * @param format the format string
63      * @param callStackOffset the offset
64      */

65     public ExtendedPatternFormatter( final String JavaDoc format, final int callStackOffset )
66     {
67         super( format );
68         m_callStackOffset = callStackOffset;
69     }
70
71     /**
72      * Retrieve the type-id for a particular string.
73      *
74      * @param type the string
75      * @return the type-id
76      */

77     protected int getTypeIdFor( final String JavaDoc type )
78     {
79         if( type.equalsIgnoreCase( TYPE_METHOD_STR ) )
80         {
81             return TYPE_METHOD;
82         }
83         else if( type.equalsIgnoreCase( TYPE_THREAD_STR ) )
84         {
85             return TYPE_THREAD;
86         }
87         else
88         {
89             return super.getTypeIdFor( type );
90         }
91     }
92
93     /**
94      * Formats a single pattern run (can be extended in subclasses).
95      *
96      * @param event the log event
97      * @param run the pattern run to format.
98      * @return the formatted result.
99      */

100     protected String JavaDoc formatPatternRun( final LogEvent event, final PatternRun run )
101     {
102         switch( run.m_type )
103         {
104             case TYPE_METHOD:
105                 return getMethod( event );
106             case TYPE_THREAD:
107                 return getThread( event );
108             default:
109                 return super.formatPatternRun( event, run );
110         }
111     }
112
113     /**
114      * Utility method to format category.
115      *
116      * @param event the event
117      * @return the formatted string
118      */

119     private String JavaDoc getMethod( final LogEvent event )
120     {
121         final ContextMap map = event.getContextMap();
122         if( null != map )
123         {
124             final Object JavaDoc object = map.get( "method" );
125             if( null != object )
126             {
127                 return object.toString();
128             }
129         }
130
131         //Determine callee of user's class. If offset is 0, we need to find
132
// Logger.class. If offset is 1, We need to find caller of Logger.class, etc.
133
final Class JavaDoc clazz = StackIntrospector.getCallerClass( Logger.class, m_callStackOffset - 1 );
134         if( null == clazz )
135         {
136             return "UnknownMethod";
137         }
138
139         final String JavaDoc result = StackIntrospector.getCallerMethod( clazz );
140         if( null == result )
141         {
142             return "UnknownMethod";
143         }
144         return result;
145     }
146
147     /**
148      * Utility thread to format category.
149      *
150      * @param event the even
151      * @return the formatted string
152      */

153     private String JavaDoc getThread( final LogEvent event )
154     {
155         final ContextMap map = event.getContextMap();
156         if( null != map )
157         {
158             final Object JavaDoc object = map.get( "thread" );
159             if( null != object )
160             {
161                 return object.toString();
162             }
163         }
164
165         return Thread.currentThread().getName();
166     }
167 }
168
Popular Tags