Skip to content

Print RuntimeException raised by StructuredLogFormatter::format to system err #43371

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -58,12 +59,26 @@ private StructuredLogLayout(Charset charset, StructuredLogFormatter<LogEvent> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
*
* @author Moritz Halbritter
* @author Phillip Webb
* @author Yanming Zhou
* @since 3.4.0
* @see StructuredLogFormatter
*/
Expand Down Expand Up @@ -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
Expand Down
Loading