001/*
002 * Copyright 2012-2017 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.boot.logging.log4j2;
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.pattern.ConverterKeys;
023import org.apache.logging.log4j.core.pattern.ExtendedThrowablePatternConverter;
024import org.apache.logging.log4j.core.pattern.PatternConverter;
025import org.apache.logging.log4j.core.pattern.ThrowablePatternConverter;
026
027/**
028 * {@link ThrowablePatternConverter} that adds some additional whitespace around the stack
029 * trace.
030 *
031 * @author Vladimir Tsanev
032 * @author Phillip Webb
033 * @since 1.3.0
034 */
035@Plugin(name = "ExtendedWhitespaceThrowablePatternConverter", category = PatternConverter.CATEGORY)
036@ConverterKeys({ "xwEx", "xwThrowable", "xwException" })
037public final class ExtendedWhitespaceThrowablePatternConverter
038                extends ThrowablePatternConverter {
039
040        private final ExtendedThrowablePatternConverter delegate;
041
042        private ExtendedWhitespaceThrowablePatternConverter(Configuration configuration,
043                        String[] options) {
044                super("WhitespaceExtendedThrowable", "throwable", options, configuration);
045                this.delegate = ExtendedThrowablePatternConverter.newInstance(configuration,
046                                options);
047        }
048
049        @Override
050        public void format(LogEvent event, StringBuilder buffer) {
051                if (event.getThrown() != null) {
052                        buffer.append(this.options.getSeparator());
053                        this.delegate.format(event, buffer);
054                        buffer.append(this.options.getSeparator());
055                }
056        }
057
058        /**
059         * Creates a new instance of the class. Required by Log4J2.
060         * @param configuration current configuration
061         * @param options pattern options, may be null. If first element is "short", only the
062         * first line of the throwable will be formatted.
063         * @return a new {@code WhitespaceThrowablePatternConverter}
064         */
065        public static ExtendedWhitespaceThrowablePatternConverter newInstance(
066                        Configuration configuration, String[] options) {
067                return new ExtendedWhitespaceThrowablePatternConverter(configuration, options);
068        }
069
070}