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.PatternConverter;
024import org.apache.logging.log4j.core.pattern.ThrowablePatternConverter;
025
026/**
027 * {@link ThrowablePatternConverter} that adds some additional whitespace around the stack
028 * trace.
029 *
030 * @author Vladimir Tsanev
031 * @since 1.3.0
032 */
033@Plugin(name = "WhitespaceThrowablePatternConverter", category = PatternConverter.CATEGORY)
034@ConverterKeys({ "wEx", "wThrowable", "wException" })
035public final class WhitespaceThrowablePatternConverter extends ThrowablePatternConverter {
036
037        private WhitespaceThrowablePatternConverter(Configuration configuration,
038                        String[] options) {
039                super("WhitespaceThrowable", "throwable", options, configuration);
040        }
041
042        @Override
043        public void format(LogEvent event, StringBuilder buffer) {
044                if (event.getThrown() != null) {
045                        buffer.append(this.options.getSeparator());
046                        super.format(event, buffer);
047                        buffer.append(this.options.getSeparator());
048                }
049        }
050
051        /**
052         * Creates a new instance of the class. Required by Log4J2.
053         * @param configuration current configuration
054         * @param options pattern options, may be null. If first element is "short", only the
055         * first line of the throwable will be formatted.
056         * @return a new {@code WhitespaceThrowablePatternConverter}
057         */
058        public static WhitespaceThrowablePatternConverter newInstance(
059                        Configuration configuration, String[] options) {
060                return new WhitespaceThrowablePatternConverter(configuration, options);
061        }
062
063}