KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > util > log > format > ExtendedPatternFormatter


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE file.
7  */

8 package org.jivesoftware.util.log.format;
9
10 import org.jivesoftware.util.Log;
11 import org.jivesoftware.util.log.ContextMap;
12 import org.jivesoftware.util.log.LogEvent;
13 import org.jivesoftware.util.log.util.StackIntrospector;
14
15 /**
16  * Formatter especially designed for debugging applications.
17  * <p/>
18  * This formatter extends the standard PatternFormatter to add
19  * two new possible expansions. These expansions are %{method}
20  * and %{thread}. In both cases the context map is first checked
21  * for values with specified key. This is to facilitate passing
22  * information about caller/thread when threads change (as in
23  * AsyncLogTarget). They then attempt to determine appropriate
24  * information dynamically.
25  *
26  * @author <a HREF="mailto:peter@apache.org">Peter Donald</a>
27  * @version CVS $Revision: 1.1 $ $Date: 2004/10/21 06:08:42 $
28  */

29 public class ExtendedPatternFormatter extends PatternFormatter {
30     private final static int TYPE_METHOD = MAX_TYPE + 1;
31     private final static int TYPE_THREAD = MAX_TYPE + 2;
32
33     private final static String JavaDoc TYPE_METHOD_STR = "method";
34     private final static String JavaDoc TYPE_THREAD_STR = "thread";
35
36     public ExtendedPatternFormatter(final String JavaDoc format) {
37         super(format);
38     }
39
40     /**
41      * Retrieve the type-id for a particular string.
42      *
43      * @param type the string
44      * @return the type-id
45      */

46     protected int getTypeIdFor(final String JavaDoc type) {
47         if (type.equalsIgnoreCase(TYPE_METHOD_STR))
48             return TYPE_METHOD;
49         else if (type.equalsIgnoreCase(TYPE_THREAD_STR))
50             return TYPE_THREAD;
51         else {
52             return super.getTypeIdFor(type);
53         }
54     }
55
56     /**
57      * Formats a single pattern run (can be extended in subclasses).
58      *
59      * @param run the pattern run to format.
60      * @return the formatted result.
61      */

62     protected String JavaDoc formatPatternRun(final LogEvent event, final PatternRun run) {
63         switch (run.m_type) {
64             case TYPE_METHOD:
65                 return getMethod(event, run.m_format);
66             case TYPE_THREAD:
67                 return getThread(event, run.m_format);
68             default:
69                 return super.formatPatternRun(event, run);
70         }
71     }
72
73     /**
74      * Utility method to format category.
75      *
76      * @param event
77      * @param format ancilliary format parameter - allowed to be null
78      * @return the formatted string
79      */

80     private String JavaDoc getMethod(final LogEvent event, final String JavaDoc format) {
81         final ContextMap map = event.getContextMap();
82         if (null != map) {
83             final Object JavaDoc object = map.get("method");
84             if (null != object) {
85                 return object.toString();
86             }
87         }
88
89 // final String result = StackIntrospector.getCallerMethod(Logger.class);
90
final String JavaDoc result = StackIntrospector.getCallerMethod(Log.class);
91         if (null == result) {
92             return "UnknownMethod";
93         }
94         return result;
95     }
96
97     /**
98      * Utility thread to format category.
99      *
100      * @param event
101      * @param format ancilliary format parameter - allowed to be null
102      * @return the formatted string
103      */

104     private String JavaDoc getThread(final LogEvent event, final String JavaDoc format) {
105         final ContextMap map = event.getContextMap();
106         if (null != map) {
107             final Object JavaDoc object = map.get("thread");
108             if (null != object) {
109                 return object.toString();
110             }
111         }
112
113         return Thread.currentThread().getName();
114     }
115 }
116
Popular Tags