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.impl;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  import java.util.Scanner;
22  
23  import org.apache.logging.log4j.core.pattern.JAnsiTextRenderer;
24  import org.apache.logging.log4j.core.pattern.PlainTextRenderer;
25  import org.apache.logging.log4j.core.pattern.TextRenderer;
26  import org.apache.logging.log4j.core.util.Loader;
27  import org.apache.logging.log4j.core.util.Patterns;
28  import org.apache.logging.log4j.status.StatusLogger;
29  import org.apache.logging.log4j.util.Strings;
30  
31  /**
32   * Contains options which control how a {@link Throwable} pattern is formatted.
33   */
34  public final class ThrowableFormatOptions {
35  
36      private static final int DEFAULT_LINES = Integer.MAX_VALUE;
37  
38      /**
39       * Default instance of {@code ThrowableFormatOptions}.
40       */
41      protected static final ThrowableFormatOptions DEFAULT = new ThrowableFormatOptions();
42  
43      /**
44       * Format the whole stack trace.
45       */
46      private static final String FULL = "full";
47  
48      /**
49       * Do not format the exception.
50       */
51      private static final String NONE = "none";
52  
53      /**
54       * Format only the first line of the throwable.
55       */
56      private static final String SHORT = "short";
57  
58      /**
59       * ANSI renderer
60       */
61      private final TextRenderer textRenderer;
62  
63      /**
64       * The number of lines to write.
65       */
66      private final int lines;
67  
68      /**
69       * The stack trace separator.
70       */
71      private final String separator;
72  
73      private final String suffix;
74  
75      /**
76       * The list of packages to filter.
77       */
78      private final List<String> ignorePackages;
79  
80      public static final String CLASS_NAME = "short.className";
81      public static final String METHOD_NAME = "short.methodName";
82      public static final String LINE_NUMBER = "short.lineNumber";
83      public static final String FILE_NAME = "short.fileName";
84      public static final String MESSAGE = "short.message";
85      public static final String LOCALIZED_MESSAGE = "short.localizedMessage";
86  
87      /**
88       * Constructs the options for printing stack trace.
89       *
90       * @param lines
91       *            The number of lines.
92       * @param separator
93       *            The stack trace separator.
94       * @param ignorePackages
95       *            The packages to filter.
96       * @param textRenderer
97       *            The ANSI renderer
98       * @param suffix
99       */
100     protected ThrowableFormatOptions(final int lines, final String separator, final List<String> ignorePackages,
101             final TextRenderer textRenderer, final String suffix) {
102         this.lines = lines;
103         this.separator = separator == null ? Strings.LINE_SEPARATOR : separator;
104         this.ignorePackages = ignorePackages;
105         this.textRenderer = textRenderer == null ? PlainTextRenderer.getInstance() : textRenderer;
106         this.suffix = suffix;
107     }
108 
109     /**
110      * Constructs the options for printing stack trace.
111      *
112      * @param packages
113      *            The packages to filter.
114      */
115     protected ThrowableFormatOptions(final List<String> packages) {
116         this(DEFAULT_LINES, null, packages, null, null);
117     }
118 
119     /**
120      * Constructs the options for printing stack trace.
121      */
122     protected ThrowableFormatOptions() {
123         this(DEFAULT_LINES, null, null, null, null);
124     }
125 
126     /**
127      * Returns the number of lines to write.
128      *
129      * @return The number of lines to write.
130      */
131     public int getLines() {
132         return this.lines;
133     }
134 
135     /**
136      * Returns the stack trace separator.
137      *
138      * @return The stack trace separator.
139      */
140     public String getSeparator() {
141         return this.separator;
142     }
143 
144     /**
145      * Returns the message rendered.
146      *
147      * @return the message rendered.
148      */
149     public TextRenderer getTextRenderer() {
150         return textRenderer;
151     }
152 
153     /**
154      * Returns the list of packages to ignore (filter out).
155      *
156      * @return The list of packages to ignore (filter out).
157      */
158     public List<String> getIgnorePackages() {
159         return this.ignorePackages;
160     }
161 
162     /**
163      * Determines if all lines should be printed.
164      *
165      * @return true for all lines, false otherwise.
166      */
167     public boolean allLines() {
168         return this.lines == DEFAULT_LINES;
169     }
170 
171     /**
172      * Determines if any lines should be printed.
173      *
174      * @return true for any lines, false otherwise.
175      */
176     public boolean anyLines() {
177         return this.lines > 0;
178     }
179 
180     /**
181      * Returns the minimum between the lines and the max lines.
182      *
183      * @param maxLines
184      *            The maximum number of lines.
185      * @return The number of lines to print.
186      */
187     public int minLines(final int maxLines) {
188         return this.lines > maxLines ? maxLines : this.lines;
189     }
190 
191     /**
192      * Determines if there are any packages to filter.
193      *
194      * @return true if there are packages, false otherwise.
195      */
196     public boolean hasPackages() {
197         return this.ignorePackages != null && !this.ignorePackages.isEmpty();
198     }
199 
200     /**
201      * {@inheritDoc}
202      */
203     @Override
204     public String toString() {
205         final StringBuilder s = new StringBuilder();
206         s.append('{')
207                 .append(allLines() ? FULL : this.lines == 2 ? SHORT : anyLines() ? String.valueOf(this.lines) : NONE)
208                 .append('}');
209         s.append("{separator(").append(this.separator).append(")}");
210         if (hasPackages()) {
211             s.append("{filters(");
212             for (final String p : this.ignorePackages) {
213                 s.append(p).append(',');
214             }
215             s.deleteCharAt(s.length() - 1);
216             s.append(")}");
217         }
218         return s.toString();
219     }
220 
221     /**
222      * Creates a new instance based on the array of options.
223      *
224      * @param options
225      *            The array of options.
226      * @return A new initialized instance.
227      */
228     public static ThrowableFormatOptions newInstance(String[] options) {
229         if (options == null || options.length == 0) {
230             return DEFAULT;
231         }
232         // NOTE: The following code is present for backward compatibility
233         // and was copied from Extended/RootThrowablePatternConverter.
234         // This supports a single option with the format:
235         // %xEx{["none"|"short"|"full"|depth],[filters(packages)}
236         // However, the convention for multiple options should be:
237         // %xEx{["none"|"short"|"full"|depth]}[{filters(packages)}]
238         if (options.length == 1 && Strings.isNotEmpty(options[0])) {
239             final String[] opts = options[0].split(Patterns.COMMA_SEPARATOR, 2);
240             final String first = opts[0].trim();
241             try (final Scanner scanner = new Scanner(first)) {
242                 if (opts.length > 1 && (first.equalsIgnoreCase(FULL) || first.equalsIgnoreCase(SHORT)
243                         || first.equalsIgnoreCase(NONE) || scanner.hasNextInt())) {
244                     options = new String[] { first, opts[1].trim() };
245                 }
246             }
247         }
248 
249         int lines = DEFAULT.lines;
250         String separator = DEFAULT.separator;
251         List<String> packages = DEFAULT.ignorePackages;
252         TextRenderer ansiRenderer = DEFAULT.textRenderer;
253         String suffix = DEFAULT.getSuffix();
254         for (final String rawOption : options) {
255             if (rawOption != null) {
256                 final String option = rawOption.trim();
257                 if (option.isEmpty()) {
258                     // continue;
259                 } else if (option.startsWith("separator(") && option.endsWith(")")) {
260                     separator = option.substring("separator(".length(), option.length() - 1);
261                 } else if (option.startsWith("filters(") && option.endsWith(")")) {
262                     final String filterStr = option.substring("filters(".length(), option.length() - 1);
263                     if (filterStr.length() > 0) {
264                         final String[] array = filterStr.split(Patterns.COMMA_SEPARATOR);
265                         if (array.length > 0) {
266                             packages = new ArrayList<>(array.length);
267                             for (String token : array) {
268                                 token = token.trim();
269                                 if (token.length() > 0) {
270                                     packages.add(token);
271                                 }
272                             }
273                         }
274                     }
275                 } else if (option.equalsIgnoreCase(NONE)) {
276                     lines = 0;
277                 } else if (option.equalsIgnoreCase(SHORT) || option.equalsIgnoreCase(CLASS_NAME)
278                         || option.equalsIgnoreCase(METHOD_NAME) || option.equalsIgnoreCase(LINE_NUMBER)
279                         || option.equalsIgnoreCase(FILE_NAME) || option.equalsIgnoreCase(MESSAGE)
280                         || option.equalsIgnoreCase(LOCALIZED_MESSAGE)) {
281                     lines = 2;
282                 } else if (option.startsWith("ansi(") && option.endsWith(")") || option.equals("ansi")) {
283                     if (Loader.isJansiAvailable()) {
284                         final String styleMapStr = option.equals("ansi") ? Strings.EMPTY
285                                 : option.substring("ansi(".length(), option.length() - 1);
286                         ansiRenderer = new JAnsiTextRenderer(new String[] { null, styleMapStr },
287                                 JAnsiTextRenderer.DefaultExceptionStyleMap);
288                     } else {
289                         StatusLogger.getLogger().warn(
290                                 "You requested ANSI exception rendering but JANSI is not on the classpath. Please see https://logging.apache.org/log4j/2.x/runtime-dependencies.html");
291                     }
292                 } else if (option.startsWith("S(") && option.endsWith(")")){
293                     suffix = option.substring("S(".length(), option.length() - 1);
294                 } else if (option.startsWith("suffix(") && option.endsWith(")")){
295                     suffix = option.substring("suffix(".length(), option.length() - 1);
296                 } else if (!option.equalsIgnoreCase(FULL)) {
297                     lines = Integer.parseInt(option);
298                 }
299             }
300         }
301         return new ThrowableFormatOptions(lines, separator, packages, ansiRenderer, suffix);
302     }
303 
304     public String getSuffix() {
305         return suffix;
306     }
307 
308 }