diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/StructuredLogLayout.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/StructuredLogLayout.java index 03fa13413546..8b349d7c673b 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/StructuredLogLayout.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/StructuredLogLayout.java @@ -43,6 +43,7 @@ * * @author Moritz Halbritter * @author Phillip Webb + * @author Yanming Zhou * @see StructuredLogFormatter */ @Plugin(name = "StructuredLogLayout", category = Node.CATEGORY, elementType = Layout.ELEMENT_TYPE) @@ -58,12 +59,26 @@ private StructuredLogLayout(Charset charset, StructuredLogFormatter fo @Override public String toSerializable(LogEvent event) { - return this.formatter.format(event); + try { + return this.formatter.format(event); + } + catch (RuntimeException ex) { + // exception can not be logged due to "Chicken-and-egg" problem + ex.printStackTrace(System.err); + throw ex; + } } @Override public byte[] toByteArray(LogEvent event) { - return this.formatter.formatAsBytes(event, (getCharset() != null) ? getCharset() : StandardCharsets.UTF_8); + try { + return this.formatter.formatAsBytes(event, (getCharset() != null) ? getCharset() : StandardCharsets.UTF_8); + } + catch (RuntimeException ex) { + // exception can not be logged due to "Chicken-and-egg" problem + ex.printStackTrace(System.err); + throw ex; + } } @PluginBuilderFactory diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/StructuredLogEncoder.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/StructuredLogEncoder.java index bcc3bbd12dd8..7f3bb459448d 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/StructuredLogEncoder.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/StructuredLogEncoder.java @@ -39,6 +39,7 @@ * * @author Moritz Halbritter * @author Phillip Webb + * @author Yanming Zhou * @since 3.4.0 * @see StructuredLogFormatter */ @@ -124,7 +125,14 @@ public byte[] headerBytes() { @Override public byte[] encode(ILoggingEvent event) { - return this.formatter.formatAsBytes(event, (this.charset != null) ? this.charset : StandardCharsets.UTF_8); + try { + return this.formatter.formatAsBytes(event, (this.charset != null) ? this.charset : StandardCharsets.UTF_8); + } + catch (RuntimeException ex) { + // exception can not be logged due to "Chicken-and-egg" problem + ex.printStackTrace(System.err); + throw ex; + } } @Override