我正在探索 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 的 fluent API 通过 addKeyValue 实现相同的目标?
logger.atDebug().addKeyValue("oldT", oldT).addKeyValue("newT", newT).log("Temperature changed.");