I'm using the official aws-lambda-java-log4j2, suggested here in a java lambda. I've noticed that uncaught exceptions are not logged with the root logger. For instance this lambda logs "handler" with log4j2 but not the thrown exception:
private static final logger = LogManager.getLogger();
public void handler() {
log.info("handler");
throw new RuntimeException("error")
}
The logs for an invocation of this function include the log4j2 info message, but the exception is logged plainly, without requestid, timestamp, or ERROR marker.
2019-10-07 11:39:02 43db1f36-0570-4b58-adc6-5c92ea62a862 INFO Example - handler
java.lang.RuntimeException: error
at Test.main(Test.java:3)
It's harder to find exceptions in logs because you can't grep for the request id or ERROR.
Is there a common pattern to emit logs using the log4j2 root logger in lambda?
I don't want to wrap my handlers with try-catch-log-rethrow.
try {
...
} catch (Throwable t) {
log.fatal(t);
throw t;
}