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.Configuration;
021    import org.apache.logging.log4j.core.config.plugins.Plugin;
022    import org.apache.logging.log4j.core.layout.PatternLayout;
023    
024    import java.util.List;
025    
026    /**
027     * Style pattern converter. Adds ANSI color styling to the result of the enclosed pattern.
028     */
029    @Plugin(name = "style", type = "Converter")
030    @ConverterKeys({"style" })
031    public final class StyleConverter extends LogEventPatternConverter {
032    
033        private final List<PatternFormatter> formatters;
034    
035        private final String style;
036    
037        /**
038         * Constructs the converter.
039         * @param formatters The PatternFormatters to generate the text to manipulate.
040         * @param styling The styling that should encapsulate the pattern.
041         */
042        private StyleConverter(final List<PatternFormatter> formatters, final String styling) {
043            super("style", "style");
044            this.formatters = formatters;
045            this.style = styling;
046        }
047    
048        /**
049         * Gets an instance of the class.
050         *
051         * @param config The current Configuration.
052         * @param options pattern options, may be null.  If first element is "short",
053         *                only the first line of the throwable will be formatted.
054         * @return instance of class.
055         */
056        public static StyleConverter newInstance(final Configuration config, final String[] options) {
057            if (options.length < 1) {
058                LOGGER.error("Incorrect number of options on style. Expected at least 1, received " + options.length);
059                return null;
060            }
061            if (options[0] == null) {
062                LOGGER.error("No pattern supplied on style");
063                return null;
064            }
065            if (options[1] == null) {
066                LOGGER.error("No style attributes provided");
067                return null;
068            }
069    
070            final PatternParser parser = PatternLayout.createPatternParser(config);
071            final List<PatternFormatter> formatters = parser.parse(options[0]);
072            final String style = AnsiEscape.createSequence(options[1].split("\\s*,\\s*"));
073            return new StyleConverter(formatters, style);
074        }
075    
076    
077        /**
078         * {@inheritDoc}
079         */
080        @Override
081        public void format(final LogEvent event, final StringBuilder toAppendTo) {
082            final StringBuilder buf = new StringBuilder();
083            for (final PatternFormatter formatter : formatters) {
084                formatter.format(event, buf);
085            }
086    
087            if (buf.length() > 0) {
088                toAppendTo.append(style).append(buf.toString()).append(AnsiEscape.getDefaultStyle());
089            }
090        }
091    }