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     */
017    package org.apache.logging.log4j.core.pattern;
018    
019    import org.apache.logging.log4j.core.LogEvent;
020    import org.apache.logging.log4j.core.config.plugins.Plugin;
021    import org.apache.logging.log4j.core.helpers.Constants;
022    import org.apache.logging.log4j.core.impl.ThrowableFormatOptions;
023    
024    import java.io.PrintWriter;
025    import java.io.StringWriter;
026    
027    
028    /**
029     * Outputs the Throwable portion of the LoggingEvent as a full stacktrace
030     * unless this converter's option is 'short', where it just outputs the first line of the trace, or if
031     * the number of lines to print is explicitly specified.
032     */
033    @Plugin(name = "ThrowablePatternConverter", category = "Converter")
034    @ConverterKeys({"ex", "throwable", "exception" })
035    public class ThrowablePatternConverter extends LogEventPatternConverter {
036    
037        /**
038         * The number of lines to write.
039         */
040        protected final ThrowableFormatOptions options;
041    
042        /**
043         * Constructor.
044         * @param name Name of converter.
045         * @param style CSS style for output.
046         * @param options options, may be null.
047         */
048        protected ThrowablePatternConverter(final String name, final String style, final String[] options) {
049            super(name, style);
050            this.options = ThrowableFormatOptions.newInstance(options);
051        }
052    
053        /**
054         * Gets an instance of the class.
055         *
056         * @param options pattern options, may be null.  If first element is "short",
057         *                only the first line of the throwable will be formatted.
058         * @return instance of class.
059         */
060        public static ThrowablePatternConverter newInstance(final String[] options) {
061            return new ThrowablePatternConverter("Throwable", "throwable", options);
062        }
063    
064        /**
065         * {@inheritDoc}
066         */
067        @Override
068        public void format(final LogEvent event, final StringBuilder toAppendTo) {
069            final Throwable t = event.getThrown();
070    
071            if (t != null && options.anyLines()) {
072                final StringWriter w = new StringWriter();
073                t.printStackTrace(new PrintWriter(w));
074                final int len = toAppendTo.length();
075                if (len > 0 && !Character.isWhitespace(toAppendTo.charAt(len - 1))) {
076                    toAppendTo.append(" ");
077                }
078                if (!options.allLines() || !Constants.LINE_SEP.equals(options.getSeparator())) {
079                    final StringBuilder sb = new StringBuilder();
080                    final String[] array = w.toString().split(Constants.LINE_SEP);
081                    final int limit = options.minLines(array.length) - 1;
082                    for (int i = 0; i <= limit; ++i) {
083                        sb.append(array[i]);
084                        if (i < limit) {
085                            sb.append(options.getSeparator());
086                        }
087                    }
088                    toAppendTo.append(sb.toString());
089    
090                } else {
091                    toAppendTo.append(w.toString());
092                }
093            }
094        }
095    
096        /**
097         * This converter obviously handles throwables.
098         *
099         * @return true.
100         */
101        @Override
102        public boolean handlesThrowable() {
103            return true;
104        }
105    }