|
16 | 16 |
|
17 | 17 | package org.springframework.boot.logging.log4j2; |
18 | 18 |
|
19 | | -import org.apache.logging.log4j.core.LogEvent; |
20 | 19 | import org.apache.logging.log4j.core.config.Configuration; |
21 | 20 | import org.apache.logging.log4j.core.config.plugins.Plugin; |
22 | 21 | import org.apache.logging.log4j.core.pattern.ConverterKeys; |
23 | | -import org.apache.logging.log4j.core.pattern.ExtendedThrowablePatternConverter; |
24 | 22 | import org.apache.logging.log4j.core.pattern.LogEventPatternConverter; |
25 | 23 | import org.apache.logging.log4j.core.pattern.PatternConverter; |
26 | 24 | import org.apache.logging.log4j.core.pattern.ThrowablePatternConverter; |
| 25 | +import org.apache.logging.log4j.core.pattern.VariablesNotEmptyReplacementConverter; |
27 | 26 | import org.jspecify.annotations.Nullable; |
28 | 27 |
|
29 | 28 | /** |
|
35 | 34 | */ |
36 | 35 | @Plugin(name = "WhitespaceThrowablePatternConverter", category = PatternConverter.CATEGORY) |
37 | 36 | @ConverterKeys({ "wEx", "wThrowable", "wException" }) |
38 | | -public final class WhitespaceThrowablePatternConverter extends LogEventPatternConverter { |
| 37 | +public final class WhitespaceThrowablePatternConverter { |
39 | 38 |
|
40 | | - private final ExtendedThrowablePatternConverter delegate; |
41 | | - |
42 | | - private final String separator; |
43 | | - |
44 | | - private WhitespaceThrowablePatternConverter(Configuration configuration, @Nullable String[] options) { |
45 | | - super("WhitespaceThrowable", "throwable"); |
46 | | - this.delegate = ExtendedThrowablePatternConverter.newInstance(configuration, options); |
47 | | - this.separator = this.delegate.getOptions().getSeparator(); |
48 | | - } |
49 | | - |
50 | | - @Override |
51 | | - public void format(LogEvent event, StringBuilder buffer) { |
52 | | - if (event.getThrown() != null) { |
53 | | - buffer.append(this.separator); |
54 | | - this.delegate.format(event, buffer); |
55 | | - buffer.append(this.separator); |
56 | | - } |
57 | | - } |
58 | | - |
59 | | - @Override |
60 | | - public boolean handlesThrowable() { |
61 | | - return true; |
| 39 | + private WhitespaceThrowablePatternConverter() { |
62 | 40 | } |
63 | 41 |
|
64 | | - /** |
65 | | - * Creates a new instance of the class. Required by Log4J2. |
66 | | - * @param configuration current configuration |
67 | | - * @param options pattern options, may be null. If first element is "short", only the |
68 | | - * first line of the throwable will be formatted. |
69 | | - * @return a new {@code WhitespaceThrowablePatternConverter} |
70 | | - */ |
71 | | - public static WhitespaceThrowablePatternConverter newInstance(Configuration configuration, |
72 | | - @Nullable String[] options) { |
73 | | - return new WhitespaceThrowablePatternConverter(configuration, options); |
| 42 | + public static LogEventPatternConverter newInstance(Configuration configuration, String @Nullable [] options) { |
| 43 | + final String nested = (options != null && options.length > 0) ? "{" + String.join("}{", options) + "}" : ""; |
| 44 | + final String pattern = "%notEmpty{%n%ex" + nested + "%n}"; |
| 45 | + return VariablesNotEmptyReplacementConverter.newInstance(configuration, new String[] { pattern }); |
74 | 46 | } |
75 | 47 |
|
76 | 48 | } |
0 commit comments