-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
EnsureExceptionHandling.java
100 lines (93 loc) · 3.4 KB
/
EnsureExceptionHandling.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/**
* Logback: the reliable, generic, fast and flexible logging framework.
* Copyright (C) 1999-2015, QOS.ch. All rights reserved.
*
* This program and the accompanying materials are dual-licensed under
* either the terms of the Eclipse Public License v1.0 as published by
* the Eclipse Foundation
*
* or (per the licensee's choosing)
*
* under the terms of the GNU Lesser General Public License version 2.1
* as published by the Free Software Foundation.
*/
package ch.qos.logback.classic.pattern;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.Context;
import ch.qos.logback.core.pattern.CompositeConverter;
import ch.qos.logback.core.pattern.Converter;
import ch.qos.logback.core.pattern.ConverterUtil;
import ch.qos.logback.core.pattern.PostCompileProcessor;
public class EnsureExceptionHandling implements PostCompileProcessor<ILoggingEvent> {
/**
* This implementation checks if any of the converters in the chain handles
* exceptions. If not, then this method adds a
* {@link ExtendedThrowableProxyConverter} instance to the end of the chain.
* <p>
* This allows appenders using this layout to output exception information event
* if the user forgets to add %ex to the pattern. Note that the appenders
* defined in the Core package are not aware of exceptions nor LoggingEvents.
* <p>
* If for some reason the user wishes to NOT print exceptions, then she can add
* %nopex to the pattern.
*
*
*/
public void process(Context context, Converter<ILoggingEvent> head) {
if (head == null) {
// this should never happen
throw new IllegalArgumentException("cannot process empty chain");
}
if (!chainHandlesThrowable(head)) {
Converter<ILoggingEvent> tail = ConverterUtil.findTail(head);
Converter<ILoggingEvent> exConverter = null;
LoggerContext loggerContext = (LoggerContext) context;
if (loggerContext.isPackagingDataEnabled()) {
exConverter = new ExtendedThrowableProxyConverter();
} else {
exConverter = new ThrowableProxyConverter();
}
tail.setNext(exConverter);
}
}
/**
* This method computes whether a chain of converters handles exceptions or not.
*
* @param head The first element of the chain
* @return true if can handle throwables contained in logging events
*/
public boolean chainHandlesThrowable(Converter<ILoggingEvent> head) {
Converter<ILoggingEvent> c = head;
while (c != null) {
if (c instanceof ThrowableHandlingConverter) {
return true;
} else if (c instanceof CompositeConverter) {
if (compositeHandlesThrowable((CompositeConverter<ILoggingEvent>) c)) {
return true;
}
}
c = c.getNext();
}
return false;
}
/**
* This method computes whether a composite converter handles exceptions or not.
*
* @param converter The composite converter
* @return true if can handle throwables contained in logging events
*/
public boolean compositeHandlesThrowable(CompositeConverter<ILoggingEvent> compositeConverter) {
Converter<ILoggingEvent> childConverter = compositeConverter.getChildConverter();
for (Converter<ILoggingEvent> c = childConverter; c != null; c = c.getNext()) {
if (c instanceof ThrowableHandlingConverter) {
return true;
} else if (c instanceof CompositeConverter) {
boolean r = compositeHandlesThrowable((CompositeConverter<ILoggingEvent>) c);
if (r)
return true;
}
}
return false;
}
}