Skip to content

Commit 75509a9

Browse files
committed
fix PR 404, LOGBACK-543
Signed-off-by: ceki <ceki@qos.ch>
1 parent 8eb9356 commit 75509a9

File tree

7 files changed

+94
-23
lines changed

7 files changed

+94
-23
lines changed

logback-classic/src/main/java/ch/qos/logback/classic/pattern/ThrowableProxyConverter.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -177,17 +177,9 @@ private void subjoinFirstLine(StringBuilder buf, String prefix, int indent, IThr
177177
if (prefix != null) {
178178
buf.append(prefix);
179179
}
180-
subjoinExceptionMessage(buf, tp);
180+
ThrowableProxyUtil.subjoinExceptionMessage(buf, tp);
181181
}
182182

183-
private void subjoinExceptionMessage(StringBuilder buf, IThrowableProxy tp) {
184-
if (tp.isCyclic()) {
185-
buf.append("[CIRCULAR REFERENCE: ").append(tp.getClassName()).append(": ").append(tp.getMessage())
186-
.append(']');
187-
} else {
188-
buf.append(tp.getClassName()).append(": ").append(tp.getMessage());
189-
}
190-
}
191183

192184
protected void subjoinSTEPArray(StringBuilder buf, int indent, IThrowableProxy tp) {
193185
StackTraceElementProxy[] stepArray = tp.getStackTraceElementProxyArray();

logback-classic/src/main/java/ch/qos/logback/classic/spi/IThrowableProxy.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,33 @@
11
/**
22
* Logback: the reliable, generic, fast and flexible logging framework.
33
* Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4-
*
4+
* <p>
55
* This program and the accompanying materials are dual-licensed under
66
* either the terms of the Eclipse Public License v1.0 as published by
77
* the Eclipse Foundation
8-
*
9-
* or (per the licensee's choosing)
10-
*
8+
* <p>
9+
* or (per the licensee's choosing)
10+
* <p>
1111
* under the terms of the GNU Lesser General Public License version 2.1
1212
* as published by the Free Software Foundation.
1313
*/
1414
package ch.qos.logback.classic.spi;
1515

1616
public interface IThrowableProxy {
17+
18+
/**
19+
* Return the overriding message if any. This method returns null
20+
* if there is no overriding message.
21+
*
22+
* <p>Overriding message exists only if the original throwable implementation overrides the toString() method.</p>
23+
*
24+
* @return the overriding message or null
25+
* @since 1.5.22
26+
*/
27+
default String getOverridingMessage() {
28+
return null;
29+
}
30+
1731
String getMessage();
1832

1933
String getClassName();
@@ -28,7 +42,7 @@ public interface IThrowableProxy {
2842

2943
/**
3044
* Is this instance the result of a cyclic exception?
31-
*
45+
*
3246
* @return true if cyclic, false otherwise
3347
* @sine 1.3.0
3448
*/

logback-classic/src/main/java/ch/qos/logback/classic/spi/ThrowableProxy.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public class ThrowableProxy implements IThrowableProxy {
2828

2929
private Throwable throwable;
3030
private String className;
31+
private String overridingMessage;
3132
private String message;
3233
// package-private because of ThrowableProxyUtil
3334
StackTraceElementProxy[] stackTraceElementProxyArray;
@@ -65,6 +66,7 @@ public ThrowableProxy(Throwable throwable, Set<Throwable> alreadyProcessedSet) {
6566
this.throwable = throwable;
6667
this.className = throwable.getClass().getName();
6768
this.message = throwable.getMessage();
69+
this.overridingMessage = buildOverridingMessage(throwable);
6870
this.stackTraceElementProxyArray = ThrowableProxyUtil.steArrayToStepArray(throwable.getStackTrace());
6971
this.cyclic = false;
7072

@@ -101,6 +103,18 @@ public ThrowableProxy(Throwable throwable, Set<Throwable> alreadyProcessedSet) {
101103
}
102104
}
103105

106+
private String buildOverridingMessage(Throwable throwable) {
107+
StringBuilder sb = new StringBuilder();
108+
ThrowableProxyUtil.appendNominalFirstLine(sb, throwable.getClass().getName(), throwable.getMessage());
109+
String messageFromToString = throwable.toString();
110+
String nominalMessage = sb.toString();
111+
if (!nominalMessage.equals(messageFromToString)) {
112+
return messageFromToString;
113+
} else {
114+
return null;
115+
}
116+
}
117+
104118
public Throwable getThrowable() {
105119
return throwable;
106120
}
@@ -109,6 +123,16 @@ public String getMessage() {
109123
return message;
110124
}
111125

126+
/*
127+
* (non-Javadoc)
128+
*
129+
* @see ch.qos.logback.classic.spi.IThrowableProxy#getOverridingMessage()
130+
*/
131+
@Override
132+
public String getOverridingMessage() {
133+
return overridingMessage;
134+
}
135+
112136
/*
113137
* (non-Javadoc)
114138
*

logback-classic/src/main/java/ch/qos/logback/classic/spi/ThrowableProxyUtil.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ static int findNumberOfCommonFrames(StackTraceElement[] steArray, StackTraceElem
7373
return count;
7474
}
7575

76+
public static void appendNominalFirstLine(StringBuilder buf, String classname, String message) {
77+
buf.append(classname).append(": ").append(message);
78+
}
79+
7680
public static String asString(IThrowableProxy tp) {
7781
StringBuilder sb = new StringBuilder(BUILDER_CAPACITY);
7882

@@ -181,12 +185,21 @@ public static void subjoinFirstLineRootCauseFirst(StringBuilder buf, IThrowableP
181185
subjoinExceptionMessage(buf, tp);
182186
}
183187

184-
private static void subjoinExceptionMessage(StringBuilder buf, IThrowableProxy tp) {
188+
public static void subjoinExceptionMessage(StringBuilder stringBuilder, IThrowableProxy tp) {
185189
if (tp.isCyclic()) {
186-
buf.append("[CIRCULAR REFERENCE: ").append(tp.getClassName()).append(": ").append(tp.getMessage())
187-
.append(']');
190+
stringBuilder.append("[CIRCULAR REFERENCE: ");
191+
appendNominalOrOverridingMessage(stringBuilder, tp);
192+
stringBuilder.append(']');
193+
} else {
194+
appendNominalOrOverridingMessage(stringBuilder, tp);
195+
}
196+
}
197+
198+
private static void appendNominalOrOverridingMessage(StringBuilder stringBuilder, IThrowableProxy tp) {
199+
if(tp.getOverridingMessage() == null) {
200+
appendNominalFirstLine(stringBuilder, tp.getClassName(), tp.getMessage());
188201
} else {
189-
buf.append(tp.getClassName()).append(": ").append(tp.getMessage());
202+
stringBuilder.append(tp.getOverridingMessage());
190203
}
191204
}
192205
}

logback-classic/src/main/java/ch/qos/logback/classic/spi/ThrowableProxyVO.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public class ThrowableProxyVO implements IThrowableProxy, Serializable {
2121
private static final long serialVersionUID = -773438177285807139L;
2222

2323
private String className;
24+
private String overridingMessage;
2425
private String message;
2526
private int commonFramesCount;
2627
private StackTraceElementProxy[] stackTraceElementProxyArray;
@@ -31,6 +32,17 @@ public class ThrowableProxyVO implements IThrowableProxy, Serializable {
3132
public String getMessage() {
3233
return message;
3334
}
35+
/**
36+
* Return the overriding message if any. This method returns null
37+
* if there is no overriding message.
38+
*
39+
* <p>Overriding message exists only if the original throwable implementation overrides the toString() method.</p>
40+
*
41+
* @return the overriding message or null
42+
* @since 1.5.22
43+
*/
44+
@Override
45+
public String getOverridingMessage() { return overridingMessage;}
3446

3547
public String getClassName() {
3648
return className;
@@ -102,6 +114,7 @@ public static ThrowableProxyVO build(IThrowableProxy throwableProxy) {
102114
ThrowableProxyVO tpvo = new ThrowableProxyVO();
103115
tpvo.className = throwableProxy.getClassName();
104116
tpvo.message = throwableProxy.getMessage();
117+
tpvo.overridingMessage = throwableProxy.getOverridingMessage();
105118
tpvo.commonFramesCount = throwableProxy.getCommonFrames();
106119
tpvo.stackTraceElementProxyArray = throwableProxy.getStackTraceElementProxyArray();
107120
tpvo.cyclic = throwableProxy.isCyclic();

logback-classic/src/test/java/ch/qos/logback/classic/spi/PubThrowableProxy.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919

2020
import java.util.Arrays;
2121

22+
/**
23+
* Used in tests to represent a ThrowableProxy in a JSON-friendly way.
24+
*/
2225
public class PubThrowableProxy implements IThrowableProxy {
2326

2427
private String className;

logback-classic/src/test/java/ch/qos/logback/classic/spi/ThrowableProxyTest.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ public void verify(Throwable t) {
4747
result = result.replace("common frames omitted", "more");
4848
String expected = sw.toString();
4949

50-
// System.out.println("========expected");
51-
// System.out.println(expected);
52-
53-
// System.out.println("========result");
54-
// System.out.println(result);
50+
// System.out.println("========expected");
51+
// System.out.println(expected);
52+
//
53+
// System.out.println("========result");
54+
// System.out.println(result);
5555

5656
assertEquals(expected, result);
5757
}
@@ -180,6 +180,16 @@ public void cyclicSuppressed() {
180180
verify(e);
181181
}
182182

183+
@Test
184+
public void overriddenToString() {
185+
Exception e = new Exception() {
186+
public String toString() {
187+
return getClass().getName() + " [extra]";
188+
}
189+
};
190+
verify(e);
191+
}
192+
183193
void someMethod() throws Exception {
184194
throw new Exception("someMethod");
185195
}
@@ -202,4 +212,6 @@ void someOtherMethod() throws Exception {
202212
throw new Exception("someOtherMethod", e);
203213
}
204214
}
215+
216+
205217
}

0 commit comments

Comments
 (0)