View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.core.pattern;
18  
19  import java.util.Arrays;
20  import java.util.HashMap;
21  import java.util.List;
22  import java.util.Locale;
23  import java.util.Map;
24  
25  import org.apache.logging.log4j.Level;
26  import org.apache.logging.log4j.core.LogEvent;
27  import org.apache.logging.log4j.core.config.Configuration;
28  import org.apache.logging.log4j.core.config.plugins.Plugin;
29  import org.apache.logging.log4j.core.layout.PatternLayout;
30  
31  /**
32   * Highlight pattern converter. Formats the result of a pattern using a color appropriate for the Level in the LogEvent.
33   * <p>
34   * For example:
35   *
36   * <pre>
37   * %highlight{%d{ ISO8601 } [%t] %-5level: %msg%n%throwable}
38   * </pre>
39   * </p>
40   *
41   * <p>
42   * You can define custom colors for each Level:
43   *
44   * <pre>
45   * %highlight{%d{ ISO8601 } [%t] %-5level: %msg%n%throwable}{FATAL=red, ERROR=red, WARN=yellow, INFO=green, DEBUG=cyan,
46   * TRACE=black}
47   * </pre>
48   * </p>
49   *
50   * <p>
51   * You can use a predefined style:
52   *
53   * <pre>
54   * %highlight{%d{ ISO8601 } [%t] %-5level: %msg%n%throwable}{STYLE=Log4j}
55   * </pre>
56   * The available predefined styles are:
57   * <ul>
58   * <li>{@code Default}</li>
59   * <li>{@code Log4j} - The same as {@code Default}</li>
60   * <li>{@code Logback}</li>
61   * </ul>
62   * </p>
63   *
64   * <p>
65   * You can use whitespace around the comma and equal sign. The names in values MUST come from the
66   * {@linkplain AnsiEscape} enum, case is
67   * normalized to upper-case internally.
68   * </p>
69   */
70  @Plugin(name = "highlight", category = "Converter")
71  @ConverterKeys({ "highlight" })
72  public final class HighlightConverter extends LogEventPatternConverter implements AnsiConverter {
73  
74      private static final Map<Level, String> DEFAULT_STYLES = new HashMap<Level, String>();
75  
76      private static final Map<Level, String> LOGBACK_STYLES = new HashMap<Level, String>();
77  
78      private static final String STYLE_KEY = "STYLE";
79  
80      private static final String STYLE_KEY_DEFAULT = "DEFAULT";
81  
82      private static final String STYLE_KEY_LOGBACK = "LOGBACK";
83  
84      private static final Map<String, Map<Level, String>> STYLES = new HashMap<String, Map<Level, String>>();
85  
86      static {
87          // Default styles:
88          DEFAULT_STYLES.put(Level.FATAL, AnsiEscape.createSequence("BRIGHT", "RED"));
89          DEFAULT_STYLES.put(Level.ERROR, AnsiEscape.createSequence("BRIGHT", "RED"));
90          DEFAULT_STYLES.put(Level.WARN, AnsiEscape.createSequence("YELLOW"));
91          DEFAULT_STYLES.put(Level.INFO, AnsiEscape.createSequence("GREEN"));
92          DEFAULT_STYLES.put(Level.DEBUG, AnsiEscape.createSequence("CYAN"));
93          DEFAULT_STYLES.put(Level.TRACE, AnsiEscape.createSequence("BLACK"));
94          // Logback styles:
95          LOGBACK_STYLES.put(Level.FATAL, AnsiEscape.createSequence("BLINK", "BRIGHT", "RED"));
96          LOGBACK_STYLES.put(Level.ERROR, AnsiEscape.createSequence("BRIGHT", "RED"));
97          LOGBACK_STYLES.put(Level.WARN, AnsiEscape.createSequence("RED"));
98          LOGBACK_STYLES.put(Level.INFO, AnsiEscape.createSequence("BLUE"));
99          LOGBACK_STYLES.put(Level.DEBUG, AnsiEscape.createSequence((String[]) null));
100         LOGBACK_STYLES.put(Level.TRACE, AnsiEscape.createSequence((String[]) null));
101         // Style map:
102         STYLES.put(STYLE_KEY_DEFAULT, DEFAULT_STYLES);
103         STYLES.put(STYLE_KEY_LOGBACK, LOGBACK_STYLES);
104     }
105 
106     /**
107      * Creates a level style map where values are ANSI escape sequences given configuration options in
108      * {@code option[1]}.
109      * <p/>
110      * The format of the option string in {@code option[1]} is:
111      *
112      * <pre>
113      * Level1=Value, Level2=Value, ...
114      * </pre>
115      *
116      * For example:
117      *
118      * <pre>
119      * ERROR=red bold, WARN=yellow bold, INFO=green, ...
120      * </pre>
121      *
122      * You can use whitespace around the comma and equal sign. The names in values MUST come from the
123      * {@linkplain AnsiEscape} enum, case is
124      * normalized to upper-case internally.
125      *
126      * @param options
127      *            The second slot can optionally contain the style map.
128      * @return a new map
129      */
130     private static Map<Level, String> createLevelStyleMap(final String[] options) {
131         if (options.length < 2) {
132             return DEFAULT_STYLES;
133         }
134         // Feels like a hack. Should String[] options change to a Map<String,String>?
135         String string = options[1].replaceAll(PatternParser.NO_CONSOLE_NO_ANSI + "=(true|false)", "");
136         //
137         final Map<String, String> styles = AnsiEscape.createMap(string, new String[] {STYLE_KEY});
138         final Map<Level, String> levelStyles = new HashMap<Level, String>(DEFAULT_STYLES);
139         for (final Map.Entry<String, String> entry : styles.entrySet()) {
140             final String key = entry.getKey().toUpperCase(Locale.ENGLISH);
141             final String value = entry.getValue();
142             if (STYLE_KEY.equalsIgnoreCase(key)) {
143                 final Map<Level, String> enumMap = STYLES.get(value.toUpperCase(Locale.ENGLISH));
144                 if (enumMap == null) {
145                     LOGGER.error("Unknown level style: " + value + ". Use one of " +
146                         Arrays.toString(STYLES.keySet().toArray()));
147                 } else {
148                     levelStyles.putAll(enumMap);
149                 }
150             } else {
151                 final Level level = Level.toLevel(key);
152                 if (level == null) {
153                     LOGGER.error("Unknown level name: " + key + ". Use one of " +
154                         Arrays.toString(DEFAULT_STYLES.keySet().toArray()));
155                 } else {
156                     levelStyles.put(level, value);
157                 }
158             }
159         }
160         return levelStyles;
161     }
162 
163     /**
164      * Gets an instance of the class.
165      *
166      * @param config The current Configuration.
167      * @param options pattern options, may be null. If first element is "short", only the first line of the
168      *                throwable will be formatted.
169      * @return instance of class.
170      */
171     public static HighlightConverter newInstance(final Configuration config, final String[] options) {
172         if (options.length < 1) {
173             LOGGER.error("Incorrect number of options on style. Expected at least 1, received " + options.length);
174             return null;
175         }
176         if (options[0] == null) {
177             LOGGER.error("No pattern supplied on style");
178             return null;
179         }
180         final PatternParser parser = PatternLayout.createPatternParser(config);
181         final List<PatternFormatter> formatters = parser.parse(options[0]);
182         return new HighlightConverter(formatters, createLevelStyleMap(options));
183     }
184 
185     private final Map<Level, String> levelStyles;
186 
187     private final List<PatternFormatter> patternFormatters;
188 
189     /**
190      * Construct the converter.
191      *
192      * @param patternFormatters
193      *            The PatternFormatters to generate the text to manipulate.
194      */
195     private HighlightConverter(final List<PatternFormatter> patternFormatters, final Map<Level, String> levelStyles) {
196         super("style", "style");
197         this.patternFormatters = patternFormatters;
198         this.levelStyles = levelStyles;
199     }
200 
201     /**
202      * {@inheritDoc}
203      */
204     @Override
205     public void format(final LogEvent event, final StringBuilder toAppendTo) {
206         final StringBuilder buf = new StringBuilder();
207         for (final PatternFormatter formatter : patternFormatters) {
208             formatter.format(event, buf);
209         }
210 
211         if (buf.length() > 0) {
212             toAppendTo.append(levelStyles.get(event.getLevel())).append(buf.toString()).
213                 append(AnsiEscape.getDefaultStyle());
214         }
215     }
216 
217     @Override
218     public boolean handlesThrowable() {
219         for (final PatternFormatter formatter : patternFormatters) {
220             if (formatter .handlesThrowable()) {
221                 return true;
222             }
223         }
224         return false;
225     }
226 }