The Golang standard library lets you do this:
slog.Info("showing named arguments with structured logging",
slog.Bool("some-bool", true),
slog.Int("some-int", 123),
slog.Float64("some-float64", 1.234),
slog.String("some-string", "xyz"))
which outputs the following at runtime:
{"time":"2024-04-07T11:10:04.925291-05:00","level":"INFO","msg":"showing
named arguments with structured
logging","some-bool":true,"some-int":123,"some-float64":1.234,"some-string":"xyz"}
I'd like to do this in Java. This seems to be a fairly common use
case. Log4j's API doesn't seem to offer this. The closest I can get
is:
Logger logger = LogManager.getLogger();
logger.info(new MapMessage()
.with("someInt", 123)
.with("someFloat", 1.23456)
.with("someBool", true)
.with("someString", "abc"));
which outputs the following at runtime. Notice that it just converted
the MapMessage to a string and put that in the JSON message field.:
{"@timestamp":"2024-04-07T16:26:21.232Z","ecs.version":"1.2.0","log.level":"INFO","message":"someBool=\"true\"
someFloat=\"1.23456\" someInt=\"123\"
someString=\"abc\"","process.thread.name":"main","log.logger":"demo.Main"}
Is it possible that log4j3 offer something like what Golang's slog does?
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]