aws-lambda - Log aws lambda handler exceptions with log4j2 using aws-lambda-java-log4j2
问题描述
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;
}
解决方案
推荐阅读
- dynamics-crm - Dynamics 8.1 和 365:“Publisher”是否可移植用于托管解决方案?
- android - 如何为 64 位 Android 构建 cordova 项目?
- algorithm - 查找与单词查询匹配的顶级文档
- java - 工厂返回带有 EJB 注入字段 Null 的 Bean
- ios - 如何在 MapKit 中限制平移和缩小苹果地图的某个区域?
- java - Freemarker 宏中的 Hashmap 不起作用
- python - 根据匹配索引合并两个数据帧以更新数据帧中的其他列
- android - 使用 espresso 为 React-native 应用程序编写 UI 测试
- python - 如何将包含短语的 str 列表转换为 int 列表?
- selenium - Chrome 开发者工具中的 Selenium IDE 在哪里?