001: /* ====================================================================
002: * The Apache Software License, Version 1.1
003: *
004: * Copyright (c) 1997-2003 The Apache Software Foundation. All rights
005: * reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The end-user documentation included with the redistribution,
020: * if any, must include the following acknowledgment:
021: * "This product includes software developed by the
022: * Apache Software Foundation (http://www.apache.org/)."
023: * Alternately, this acknowledgment may appear in the software
024: * itself, if and wherever such third-party acknowledgments
025: * normally appear.
026: *
027: * 4. The names "Jakarta", "Avalon", and "Apache Software Foundation"
028: * must not be used to endorse or promote products derived from this
029: * software without prior written permission. For written
030: * permission, please contact apache@apache.org.
031: *
032: * 5. Products derived from this software may not be called "Apache",
033: * nor may "Apache" appear in their name, without prior written
034: * permission of the Apache Software Foundation.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
040: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: * ====================================================================
049: *
050: * This software consists of voluntary contributions made by many
051: * individuals on behalf of the Apache Software Foundation. For more
052: * information on the Apache Software Foundation, please see
053: * <http://www.apache.org/>.
054: */
055: package org.apache.log.format;
056:
057: import org.apache.log.LogEvent;
058: import org.apache.log.format.ExtendedPatternFormatter;
059: import org.apache.log.format.PatternFormatter;
060: import org.apache.log.util.StackIntrospector;
061:
062: /**
063: * This formatter extends ExtendedPatternFormatter so that
064: * CascadingExceptions are formatted with all nested exceptions.
065: *
066: * <ul>
067: * <li><code>class</code> : outputs the name of the class that has logged the
068: * message. The optional <code>short</code> subformat removes the
069: * package name. Warning : this pattern works only if formatting occurs in
070: * the same thread as the call to Logger, i.e. it won't work with
071: * <code>AsyncLogTarget</code>.</li>
072: * </ul>
073: *
074: * @deprecated Use <code>org.apache.avalon.framework.logger.AvalonFormatter</code>
075: * instead of this one.
076: * @author <a href="mailto:dev@avalon.apache.org">Avalon Development Team</a>
077: */
078: public class AvalonFormatter extends ExtendedPatternFormatter {
079: private static final int TYPE_CLASS = MAX_TYPE + 1;
080:
081: private static final String TYPE_CLASS_STR = "class";
082: private static final String TYPE_CLASS_SHORT_STR = "short";
083:
084: /**
085: * The constant defining the default stack depth when
086: * none other is specified.
087: */
088: public static final int DEFAULT_STACK_DEPTH = 8;
089:
090: /**
091: * The constant defining the default behaviour for printing
092: * nested exceptions.
093: */
094: public static final boolean DEFAULT_PRINT_CASCADING = true;
095:
096: //The depth to which stacktraces are printed out
097: private final int m_stackDepth;
098:
099: //Determines if nested exceptions should be logged
100: private final boolean m_printCascading;
101:
102: /**
103: * Construct the formatter with the specified pattern
104: * and which which prints out exceptions to stackDepth of 8.
105: *
106: * @param pattern The pattern to use to format the log entries
107: */
108: public AvalonFormatter(final String pattern) {
109: this (pattern, DEFAULT_STACK_DEPTH, DEFAULT_PRINT_CASCADING);
110: }
111:
112: /**
113: * Construct the formatter with the specified pattern
114: * and which which prints out exceptions to stackDepth specified.
115: *
116: * @param pattern The pattern to use to format the log entries
117: * @param stackDepth The depth to which stacktraces are printed out
118: * @param printCascading true enables printing of nested exceptions,
119: * false only prints out the outermost exception
120: */
121: public AvalonFormatter(final String pattern, final int stackDepth,
122: final boolean printCascading) {
123: super (pattern);
124: m_stackDepth = stackDepth;
125: m_printCascading = printCascading;
126: }
127:
128: /**
129: * Utility method to format stack trace.
130: *
131: * @param throwable the throwable instance
132: * @param format ancilliary format parameter - allowed to be null
133: * @return the formatted string
134: */
135: protected String getStackTrace(final Throwable throwable,
136: final String format) {
137: if (null == throwable) {
138: return "";
139: }
140: return ExceptionUtil.printStackTrace(throwable, m_stackDepth,
141: m_printCascading);
142: }
143:
144: /**
145: * Retrieve the type-id for a particular string.
146: *
147: * @param type the string
148: * @return the type-id
149: */
150: protected int getTypeIdFor(final String type) {
151: if (type.equalsIgnoreCase(TYPE_CLASS_STR)) {
152: return TYPE_CLASS;
153: } else {
154: return super .getTypeIdFor(type);
155: }
156: }
157:
158: protected String formatPatternRun(LogEvent event,
159: PatternFormatter.PatternRun run) {
160: switch (run.m_type) {
161: case TYPE_CLASS:
162: return getClass(run.m_format);
163: default:
164: return super .formatPatternRun(event, run);
165: }
166: }
167:
168: /**
169: * Finds the class that has called Logger.
170: */
171: private String getClass(String format) {
172: return "Unknown-class";
173: }
174: }
|