Skip to content

Commit ecfdb7f

Browse files
committed
Print RuntimeException raised by StructuredLogFormatter::format to system err
Before this commit, runtime exception is swallowed silently. For example, if two `StructuredLoggingJsonMembersCustomizer` add same key, exception is printed now: ``` Exception in thread "main" java.lang.IllegalStateException: The name 'test' has already been written at org.springframework.util.Assert.state(Assert.java:101) at org.springframework.boot.json.JsonValueWriter.writePair(JsonValueWriter.java:228) at org.springframework.boot.json.JsonValueWriter.write(JsonValueWriter.java:83) at org.springframework.boot.json.JsonWriter$Member.write(JsonWriter.java:650) at org.springframework.boot.json.JsonWriter$Members.write(JsonWriter.java:339) ```
1 parent 3ddfd62 commit ecfdb7f

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/StructuredLogLayout.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
*
4444
* @author Moritz Halbritter
4545
* @author Phillip Webb
46+
* @author Yanming Zhou
4647
* @see StructuredLogFormatter
4748
*/
4849
@Plugin(name = "StructuredLogLayout", category = Node.CATEGORY, elementType = Layout.ELEMENT_TYPE)
@@ -58,12 +59,26 @@ private StructuredLogLayout(Charset charset, StructuredLogFormatter<LogEvent> fo
5859

5960
@Override
6061
public String toSerializable(LogEvent event) {
61-
return this.formatter.format(event);
62+
try {
63+
return this.formatter.format(event);
64+
}
65+
catch (RuntimeException ex) {
66+
// exception can not be logged due to "Chicken-and-egg" problem
67+
ex.printStackTrace(System.err);
68+
throw ex;
69+
}
6270
}
6371

6472
@Override
6573
public byte[] toByteArray(LogEvent event) {
66-
return this.formatter.formatAsBytes(event, (getCharset() != null) ? getCharset() : StandardCharsets.UTF_8);
74+
try {
75+
return this.formatter.formatAsBytes(event, (getCharset() != null) ? getCharset() : StandardCharsets.UTF_8);
76+
}
77+
catch (RuntimeException ex) {
78+
// exception can not be logged due to "Chicken-and-egg" problem
79+
ex.printStackTrace(System.err);
80+
throw ex;
81+
}
6782
}
6883

6984
@PluginBuilderFactory

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/StructuredLogEncoder.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
*
4040
* @author Moritz Halbritter
4141
* @author Phillip Webb
42+
* @author Yanming Zhou
4243
* @since 3.4.0
4344
* @see StructuredLogFormatter
4445
*/
@@ -124,7 +125,14 @@ public byte[] headerBytes() {
124125

125126
@Override
126127
public byte[] encode(ILoggingEvent event) {
127-
return this.formatter.formatAsBytes(event, (this.charset != null) ? this.charset : StandardCharsets.UTF_8);
128+
try {
129+
return this.formatter.formatAsBytes(event, (this.charset != null) ? this.charset : StandardCharsets.UTF_8);
130+
}
131+
catch (RuntimeException ex) {
132+
// exception can not be logged due to "Chicken-and-egg" problem
133+
ex.printStackTrace(System.err);
134+
throw ex;
135+
}
128136
}
129137

130138
@Override

0 commit comments

Comments
 (0)