I gave this one a try. Go in a loop while trying to print a stack
trace, in case of an exception, set is as the effective exception, and
retry. Though when I started thinking more about this. I need to
repeat the same for class name, file name, line number, etc. Though
all these are independent resolvers, in every layout, including
PatternLayout. Consider the following snippet:
Throwable unprintableException =
new RuntimeException() {
@Override
public String getMessage() {
throw new RuntimeException("Doh!");
}
};
LogEvent logEvent = Log4jLogEvent
.newBuilder()
.setLoggerName("foo")
.setThrown(unprintableException)
.build();
PatternLayout layout = PatternLayout
.newBuilder()
.setConfiguration(new DefaultConfiguration())
.setPattern("%ex{short.message} %ex{short.className}")
.build();
String message = new String(layout.toByteArray(logEvent));
Let's assume we have fixed the relevant PatternLayout converters to
execute the retry logic I have aforementioned. Then %ex{short.message}
will emit the message of RuntimeException("Doh!"), whilst
%ex{short.className} will emit the class name of unprintableException.
There is also no way to get an indication of this misalignment from
the layout output.
Hence I am really puzzled about the right approach to "the code should
be modified to catch the exception and gracefully handle it".
On Wed, Jun 17, 2020 at 5:43 PM Ralph Goers <[email protected]> wrote:
> I would suggest that the code should be modified to catch the exception and
> gracefully handle it.