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.util.Constants;
24  import org.apache.logging.log4j.core.util.Patterns;
25  import org.apache.logging.log4j.util.Strings;
26  
27  /**
28   * Contains options which control how a {@link Throwable} pattern is formatted.
29   */
30  public final class ThrowableFormatOptions {
31  
32      private static final int DEFAULT_LINES = Integer.MAX_VALUE;
33  
34      /**
35       * Default instance of {@code ThrowableFormatOptions}.
36       */
37      protected static final ThrowableFormatOptions DEFAULT = new ThrowableFormatOptions();
38  
39      /**
40       * Format the whole stack trace.
41       */
42      private static final String FULL = "full";
43  
44      /**
45       * Do not format the exception.
46       */
47      private static final String NONE = "none";
48  
49      /**
50       * Format only the first line of the throwable.
51       */
52      private static final String SHORT = "short";
53  
54      /**
55       * The number of lines to write.
56       */
57      private final int lines;
58  
59      /**
60       * The stack trace separator.
61       */
62      private final String separator;
63  
64      /**
65       * The list of packages to filter.
66       */
67      private final List<String> packages;
68  
69      public static final String CLASS_NAME = "short.className";
70      public static final String METHOD_NAME = "short.methodName";
71      public static final String LINE_NUMBER = "short.lineNumber";
72      public static final String FILE_NAME = "short.fileName";
73      public static final String MESSAGE = "short.message";
74      public static final String LOCALIZED_MESSAGE = "short.localizedMessage";
75  
76      /**
77       * Construct the options for printing stack trace.
78       * @param lines The number of lines.
79       * @param separator The stack trace separator.
80       * @param packages The packages to filter.
81       */
82      protected ThrowableFormatOptions(final int lines, final String separator, final List<String> packages) {
83          this.lines = lines;
84          this.separator = separator == null ? Constants.LINE_SEPARATOR : separator;
85          this.packages = packages;
86      }
87  
88      /**
89       * Construct the options for printing stack trace.
90       * @param packages The packages to filter.
91       */
92      protected ThrowableFormatOptions(final List<String> packages) {
93          this(DEFAULT_LINES, null, packages);
94      }
95  
96      /**
97       * Construct the options for printing stack trace.
98       */
99      protected ThrowableFormatOptions() {
100         this(DEFAULT_LINES, null, null);
101     }
102 
103     /**
104      * Returns the number of lines to write.
105      * @return The number of lines to write.
106      */
107     public int getLines() {
108         return this.lines;
109     }
110 
111     /**
112      * Returns the stack trace separator.
113      * @return The stack trace separator.
114      */
115     public String getSeparator() {
116         return this.separator;
117     }
118 
119     /**
120      * Returns the list of packages to filter.
121      * @return The list of packages to filter.
122      */
123     public List<String> getPackages() {
124         return this.packages;
125     }
126 
127     /**
128      * Determines if all lines should be printed.
129      * @return true for all lines, false otherwise.
130      */
131     public boolean allLines() {
132         return this.lines == DEFAULT_LINES;
133     }
134 
135     /**
136      * Determines if any lines should be printed.
137      * @return true for any lines, false otherwise.
138      */
139     public boolean anyLines() {
140         return this.lines > 0;
141     }
142 
143     /**
144      * Returns the minimum between the lines and the max lines.
145      * @param maxLines The maximum number of lines.
146      * @return The number of lines to print.
147      */
148     public int minLines(final int maxLines) {
149         return this.lines > maxLines ? maxLines : this.lines;
150     }
151 
152     /**
153      * Determines if there are any packages to filter.
154      * @return true if there are packages, false otherwise.
155      */
156     public boolean hasPackages() {
157         return this.packages != null && !this.packages.isEmpty();
158     }
159 
160     /**
161      * {@inheritDoc}
162      */
163     @Override
164     public String toString() {
165         final StringBuilder s = new StringBuilder();
166         s.append('{').append(allLines() ? FULL : this.lines == 2 ? SHORT : anyLines() ? String.valueOf(this.lines) : NONE).append('}');
167         s.append("{separator(").append(this.separator).append(")}");
168         if (hasPackages()) {
169             s.append("{filters(");
170             for (final String p : this.packages) {
171                 s.append(p).append(',');
172             }
173             s.deleteCharAt(s.length() - 1);
174             s.append(")}");
175         }
176         return s.toString();
177     }
178 
179     /**
180      * Create a new instance based on the array of options.
181      * @param options The array of options.
182      */
183     public static ThrowableFormatOptions newInstance(String[] options) {
184         if (options == null || options.length == 0) {
185             return DEFAULT;
186         }
187         // NOTE: The following code is present for backward compatibility
188         // and was copied from Extended/RootThrowablePatternConverter.
189         // This supports a single option with the format:
190         //     %xEx{["none"|"short"|"full"|depth],[filters(packages)}
191         // However, the convention for multiple options should be:
192         //     %xEx{["none"|"short"|"full"|depth]}[{filters(packages)}]
193         if (options.length == 1 && Strings.isNotEmpty(options[0])) {
194             final String[] opts = options[0].split(Patterns.COMMA_SEPARATOR, 2);
195             final String first = opts[0].trim();
196             try (final Scanner scanner = new Scanner(first)) {
197                 if (opts.length > 1
198                         && (first.equalsIgnoreCase(FULL) || first.equalsIgnoreCase(SHORT)
199                                 || first.equalsIgnoreCase(NONE) || scanner.hasNextInt())) {
200                     options = new String[] {
201                             first,
202                             opts[1].trim() };
203                 }
204             }
205         }
206 
207         int lines = DEFAULT.lines;
208         String separator = DEFAULT.separator;
209         List<String> packages = DEFAULT.packages;
210         for (final String rawOption : options) {
211             if (rawOption != null) {
212                 final String option = rawOption.trim();
213                 if (option.isEmpty()) {
214                     // continue;
215                 } else if (option.startsWith("separator(") && option.endsWith(")")) {
216                     separator = option.substring("separator(".length(), option.length() - 1);
217                 } else if (option.startsWith("filters(") && option.endsWith(")")) {
218                     final String filterStr = option.substring("filters(".length(), option.length() - 1);
219                     if (filterStr.length() > 0) {
220                         final String[] array = filterStr.split(Patterns.COMMA_SEPARATOR);
221                         if (array.length > 0) {
222                             packages = new ArrayList<>(array.length);
223                             for (String token : array) {
224                                 token = token.trim();
225                                 if (token.length() > 0) {
226                                     packages.add(token);
227                                 }
228                             }
229                         }
230                     }
231                 } else if (option.equalsIgnoreCase(NONE)) {
232                     lines = 0;
233                 } else if (option.equalsIgnoreCase(SHORT) || option.equalsIgnoreCase(CLASS_NAME) ||
234                         option.equalsIgnoreCase(METHOD_NAME) || option.equalsIgnoreCase(LINE_NUMBER) ||
235                         option.equalsIgnoreCase(FILE_NAME) || option.equalsIgnoreCase(MESSAGE) ||
236                         option.equalsIgnoreCase(LOCALIZED_MESSAGE)) {
237                     lines = 2;
238                 } else if (!option.equalsIgnoreCase(FULL)) {
239                     lines = Integer.parseInt(option);
240                 }
241             }
242         }
243         return new ThrowableFormatOptions(lines, separator, packages);
244     }
245 }