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 java.util.Arrays; 020import java.util.List; 021 022import org.apache.logging.log4j.core.LogEvent; 023import org.apache.logging.log4j.core.config.Configuration; 024import org.apache.logging.log4j.core.config.plugins.Plugin; 025import org.apache.logging.log4j.core.layout.PatternLayout; 026import org.apache.logging.log4j.core.util.Patterns; 027 028/** 029 * Style pattern converter. Adds ANSI color styling to the result of the enclosed pattern. 030 */ 031@Plugin(name = "style", category = PatternConverter.CATEGORY) 032@ConverterKeys({ "style" }) 033public final class StyleConverter extends LogEventPatternConverter implements AnsiConverter { 034 035 /** 036 * Gets an instance of the class. 037 * 038 * @param config 039 * The current Configuration. 040 * @param options 041 * pattern options, may be null. If first element is "short", only the first line of the throwable will 042 * be formatted. 043 * @return instance of class. 044 */ 045 public static StyleConverter newInstance(final Configuration config, final String[] options) { 046 if (options.length < 1) { 047 LOGGER.error("Incorrect number of options on style. Expected at least 1, received " + options.length); 048 return null; 049 } 050 if (options[0] == null) { 051 LOGGER.error("No pattern supplied on style"); 052 return null; 053 } 054 if (options[1] == null) { 055 LOGGER.error("No style attributes provided"); 056 return null; 057 } 058 final PatternParser parser = PatternLayout.createPatternParser(config); 059 final List<PatternFormatter> formatters = parser.parse(options[0]); 060 final String style = AnsiEscape.createSequence(options[1].split(Patterns.COMMA_SEPARATOR)); 061 final boolean noConsoleNoAnsi = Arrays.toString(options).contains(PatternParser.NO_CONSOLE_NO_ANSI + "=true"); 062 final boolean hideAnsi = noConsoleNoAnsi && System.console() == null; 063 return new StyleConverter(formatters, style, hideAnsi); 064 } 065 066 private final List<PatternFormatter> patternFormatters; 067 068 private final boolean noAnsi; 069 070 private final String style; 071 072 private final String defaultStyle; 073 074 /** 075 * Constructs the converter. 076 * 077 * @param patternFormatters 078 * The PatternFormatters to generate the text to manipulate. 079 * @param style 080 * The style that should encapsulate the pattern. 081 * @param noAnsi 082 * If true, do not output ANSI escape codes. 083 */ 084 private StyleConverter(final List<PatternFormatter> patternFormatters, final String style, final boolean noAnsi) { 085 super("style", "style"); 086 this.patternFormatters = patternFormatters; 087 this.style = style; 088 this.defaultStyle = AnsiEscape.getDefaultStyle(); 089 this.noAnsi = noAnsi; 090 } 091 092 /** 093 * {@inheritDoc} 094 */ 095 @Override 096 public void format(final LogEvent event, final StringBuilder toAppendTo) { 097 final StringBuilder buf = new StringBuilder(); 098 for (final PatternFormatter formatter : patternFormatters) { 099 formatter.format(event, buf); 100 } 101 102 if (buf.length() > 0) { 103 if (noAnsi) { 104 // faster to test and do this than setting style and defaultStyle to empty strings. 105 toAppendTo.append(buf.toString()); 106 } else { 107 toAppendTo.append(style).append(buf.toString()).append(defaultStyle); 108 } 109 } 110 } 111 112 @Override 113 public boolean handlesThrowable() { 114 for (final PatternFormatter formatter : patternFormatters) { 115 if (formatter.handlesThrowable()) { 116 return true; 117 } 118 } 119 return false; 120 } 121 122 /** 123 * Returns a String suitable for debugging. 124 * 125 * @return a String suitable for debugging. 126 */ 127 @Override 128 public String toString() { 129 final StringBuilder sb = new StringBuilder(); 130 sb.append(super.toString()); 131 sb.append("[style="); 132 sb.append(style); 133 sb.append(", defaultStyle="); 134 sb.append(defaultStyle); 135 sb.append(", patternFormatters="); 136 sb.append(patternFormatters); 137 sb.append(", noAnsi="); 138 sb.append(noAnsi); 139 sb.append(']'); 140 return sb.toString(); 141 } 142 143}