[英]Log4j2 (and SLF4j 2.0.0-alpha1) and JsonTemplateLayout--how to serialize Messages as JSON
我正在探索 Log4j 2.14.0 和 SLF4j 2.0 並嘗試生成結構化消息。
我已經用稍微修改的 LogstashJsonEventLayoutV1.json 設置了我的 Appender,
<JsonTemplateLayout eventTemplateUri="classpath:LogstashJsonEventLayoutV1-test.json" properties="true" />
我刪除了時間戳和主機名(我將其作為單元測試的一部分)並修改了“消息”的配置,如下所示:
"message": {
"$resolver": "message",
"fallbackKey": "formattedMessage"}
當我記錄一些東西時
log4jLogger.atInfo().log(new MapMessage(Map.of("hello", "world")));
它顯然在生成 JSONified 日志消息:
{"@version":1,"message":{"hello":"world"},"thread_name":"Test worker","level":"INFO","logger_name":"java.lang.Integer"}
在生產中,我的商店通常通過 SLF4J 使用 Log4J。 我願意使用 SLF4J 的 2.0.0-alpha1 版本來實現這個目標。 我將如何通過 SLF4J 的流利 API 通過 addKeyValue 實現相同的目標?
logger.atDebug().addKeyValue("oldT", oldT).addKeyValue("newT", newT).log("Temperature changed.");
在一天結束時,我只是包裝了 log4j——對於這個用例,當我可以只針對 log4j 時,沒有用於包裝 Slf4j 的甘露。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.