001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.log4j.core.pattern;
018
019import org.apache.logging.log4j.core.LogEvent;
020import org.apache.logging.log4j.core.config.Configuration;
021import org.apache.logging.log4j.core.config.plugins.Plugin;
022import org.apache.logging.log4j.core.impl.ThrowableProxy;
023
024/**
025 * Outputs the Throwable portion of the LoggingEvent as a full stack trace
026 * unless this converter's option is 'short', where it just outputs the first line of the trace, or if
027 * the number of lines to print is explicitly specified.
028 * <p>
029 * The extended stack trace will also include the location of where the class was loaded from and the
030 * version of the jar if available.
031 */
032@Plugin(name = "ExtendedThrowablePatternConverter", category = PatternConverter.CATEGORY)
033@ConverterKeys({ "xEx", "xThrowable", "xException" })
034public final class ExtendedThrowablePatternConverter extends ThrowablePatternConverter {
035
036    /**
037     * Private constructor.
038     *
039     * @param config
040     * @param options options, may be null.
041     */
042    private ExtendedThrowablePatternConverter(final Configuration config, final String[] options) {
043        super("ExtendedThrowable", "throwable", options, config);
044    }
045
046    /**
047     * Gets an instance of the class.
048     *
049     * @param config The current Configuration.
050     * @param options pattern options, may be null.  If first element is "short",
051     *                only the first line of the throwable will be formatted.
052     * @return instance of class.
053     */
054    public static ExtendedThrowablePatternConverter newInstance(final Configuration config, final String[] options) {
055        return new ExtendedThrowablePatternConverter(config, options);
056    }
057
058    /**
059     * {@inheritDoc}
060     */
061    @Override
062    public void format(final LogEvent event, final StringBuilder toAppendTo) {
063        final ThrowableProxy proxy = event.getThrownProxy();
064        final Throwable throwable = event.getThrown();
065        if ((throwable != null || proxy != null) && options.anyLines()) {
066            if (proxy == null) {
067                super.format(event, toAppendTo);
068                return;
069            }
070            final int len = toAppendTo.length();
071            if (len > 0 && !Character.isWhitespace(toAppendTo.charAt(len - 1))) {
072                toAppendTo.append(' ');
073            }
074            proxy.formatExtendedStackTraceTo(toAppendTo, options.getIgnorePackages(),
075                    options.getTextRenderer(), getSuffix(event), options.getSeparator());
076        }
077    }
078
079}