zio/zio-logging

ZIO.logAnnotate and LogAnnotation behave differently

Ellzord opened this issue · 2 comments

For the SLF4J logger + logstash the following code gives inconsistent results:

ZIO.logInfo("hello world") @@ ZIOAspect.annotated("username", "Ellzord") @@ LogAnnotation.UserId("1234")
{
  "@timestamp": "2022-05-12T10:42:22.596+01:00",
  "@version": "1",
  "message": "hello world user_id=1234",
  "logger_name": "zio-slf4j-logger",
  "thread_name": "zio-default-blocking-2",
  "level": "INFO",
  "level_value": 20000,
  "username": "Ellzord"
}

Using LogAnnotation it'll only appear if you provide that to the LogFormat and then it'll be only in the message. While any annotations are given their own place in the JSON. I believe they should work the same and it should not be included in the message at all (i.e. no need to supply to LogFormat and they're counted as annotations). The naming is confusing if they are meant not to be the same thing.

It may also be possible to remove the need for the LogContext ref if they can become annotations. They would then share most of the same implementation and it doesn't matter if the user wants to use ZIO.logAnnotate or LogAnnotation (with a non-string type).

Currently I feel the only usable option is ZIO.logAnnotate as I don't believe the message should be mutated and it helps for indexing.

propagation of annotations to MDC context was fixed in 2.1.0 version,
at this level zio LogAnnotation-s and zio-logging LogAnnotation-s got same behavior, documentation/example: slf4j logger annotations

LGTM