001: /*
002: * Copyright (C) The Apache Software Foundation. All rights reserved.
003: *
004: * This software is published under the terms of the Apache Software License
005: * version 1.1, a copy of which has been included with this distribution in
006: * the LICENSE file.
007: */
008: package org.jivesoftware.util.log.format;
009:
010: import org.jivesoftware.util.Log;
011: import org.jivesoftware.util.log.ContextMap;
012: import org.jivesoftware.util.log.LogEvent;
013: import org.jivesoftware.util.log.util.StackIntrospector;
014:
015: /**
016: * Formatter especially designed for debugging applications.
017: * <p/>
018: * This formatter extends the standard PatternFormatter to add
019: * two new possible expansions. These expansions are %{method}
020: * and %{thread}. In both cases the context map is first checked
021: * for values with specified key. This is to facilitate passing
022: * information about caller/thread when threads change (as in
023: * AsyncLogTarget). They then attempt to determine appropriate
024: * information dynamically.
025: *
026: * @author <a href="mailto:peter@apache.org">Peter Donald</a>
027: * @version CVS $Revision: 37 $ $Date: 2004-10-20 23:08:43 -0700 (Wed, 20 Oct 2004) $
028: */
029: public class ExtendedPatternFormatter extends PatternFormatter {
030: private final static int TYPE_METHOD = MAX_TYPE + 1;
031: private final static int TYPE_THREAD = MAX_TYPE + 2;
032:
033: private final static String TYPE_METHOD_STR = "method";
034: private final static String TYPE_THREAD_STR = "thread";
035:
036: public ExtendedPatternFormatter(final String format) {
037: super (format);
038: }
039:
040: /**
041: * Retrieve the type-id for a particular string.
042: *
043: * @param type the string
044: * @return the type-id
045: */
046: protected int getTypeIdFor(final String type) {
047: if (type.equalsIgnoreCase(TYPE_METHOD_STR))
048: return TYPE_METHOD;
049: else if (type.equalsIgnoreCase(TYPE_THREAD_STR))
050: return TYPE_THREAD;
051: else {
052: return super .getTypeIdFor(type);
053: }
054: }
055:
056: /**
057: * Formats a single pattern run (can be extended in subclasses).
058: *
059: * @param run the pattern run to format.
060: * @return the formatted result.
061: */
062: protected String formatPatternRun(final LogEvent event,
063: final PatternRun run) {
064: switch (run.m_type) {
065: case TYPE_METHOD:
066: return getMethod(event, run.m_format);
067: case TYPE_THREAD:
068: return getThread(event, run.m_format);
069: default:
070: return super .formatPatternRun(event, run);
071: }
072: }
073:
074: /**
075: * Utility method to format category.
076: *
077: * @param event
078: * @param format ancilliary format parameter - allowed to be null
079: * @return the formatted string
080: */
081: private String getMethod(final LogEvent event, final String format) {
082: final ContextMap map = event.getContextMap();
083: if (null != map) {
084: final Object object = map.get("method");
085: if (null != object) {
086: return object.toString();
087: }
088: }
089:
090: // final String result = StackIntrospector.getCallerMethod(Logger.class);
091: final String result = StackIntrospector
092: .getCallerMethod(Log.class);
093: if (null == result) {
094: return "UnknownMethod";
095: }
096: return result;
097: }
098:
099: /**
100: * Utility thread to format category.
101: *
102: * @param event
103: * @param format ancilliary format parameter - allowed to be null
104: * @return the formatted string
105: */
106: private String getThread(final LogEvent event, final String format) {
107: final ContextMap map = event.getContextMap();
108: if (null != map) {
109: final Object object = map.get("thread");
110: if (null != object) {
111: return object.toString();
112: }
113: }
114:
115: return Thread.currentThread().getName();
116: }
117: }
|