@@ -103,6 +103,8 @@ public class JsonEncoder extends EncoderBase<ILoggingEvent> {
103
103
104
104
private static final char VALUE_SEPARATOR = COMMA_CHAR ;
105
105
106
+ private static final String NEWLINE = "\\ \\ n" ;
107
+
106
108
private boolean withSequenceNumber = true ;
107
109
108
110
private boolean withTimestamp = true ;
@@ -119,6 +121,7 @@ public class JsonEncoder extends EncoderBase<ILoggingEvent> {
119
121
private boolean withMessage = true ;
120
122
private boolean withArguments = true ;
121
123
private boolean withThrowable = true ;
124
+ private boolean withRawThrowable = false ;
122
125
private boolean withFormattedMessage = false ;
123
126
124
127
@@ -190,6 +193,11 @@ public byte[] encode(ILoggingEvent event) {
190
193
if (withArguments )
191
194
appendArgumentArray (sb , event );
192
195
196
+ if (withRawThrowable ) {
197
+ appenderMember (sb , "rawThrowable" , event .getThrowableProxy () != null ? getRawThrowable (event .getThrowableProxy ()) : NULL_STR );
198
+ sb .append (VALUE_SEPARATOR );
199
+ }
200
+
193
201
if (withThrowable )
194
202
appendThrowableProxy (sb , THROWABLE_ATTR_NAME , event .getThrowableProxy ());
195
203
@@ -298,6 +306,26 @@ private void appendThrowableProxy(StringBuilder sb, String attributeName, IThrow
298
306
299
307
}
300
308
309
+ private static String getRawThrowable (IThrowableProxy throwableProxy ) {
310
+ StringBuilder sb = new StringBuilder ();
311
+ getRawThrowable (throwableProxy , sb , 0 );
312
+ return sb .toString ();
313
+ }
314
+
315
+ private static void getRawThrowable (IThrowableProxy throwable , StringBuilder sb , int depth ) {
316
+ if (throwable == null ) {
317
+ return ;
318
+ }
319
+ if (depth > 0 ) {
320
+ sb .append ("Caused by: " );
321
+ }
322
+ sb .append (throwable .getClassName ()).append (": " ).append (throwable .getMessage ()).append (NEWLINE );
323
+ for (StackTraceElementProxy step : throwable .getStackTraceElementProxyArray ()) {
324
+ sb .append (" " ).append (step ).append (NEWLINE );
325
+ }
326
+ getRawThrowable (throwable .getCause (), sb , depth + 1 );
327
+ }
328
+
301
329
private void appendSTEPArray (StringBuilder sb , StackTraceElementProxy [] stepArray , int commonFrames ) {
302
330
sb .append (QUOTE ).append (STEP_ARRAY_NAME_ATTRIBUTE ).append (QUOTE_COL ).append (OPEN_ARRAY );
303
331
@@ -512,8 +540,15 @@ public void setWithThrowable(boolean withThrowable) {
512
540
this .withThrowable = withThrowable ;
513
541
}
514
542
543
+ /**
544
+ * @param withRawThrowable
545
+ * @since 1.5.7
546
+ */
547
+ public void setWithRawThrowable (boolean withRawThrowable ) {
548
+ this .withRawThrowable = withRawThrowable ;
549
+ }
550
+
515
551
public void setWithFormattedMessage (boolean withFormattedMessage ) {
516
552
this .withFormattedMessage = withFormattedMessage ;
517
553
}
518
-
519
554
}
0 commit comments