From 94c14273a74dd20e254bdf70c550391cb0856db9 Mon Sep 17 00:00:00 2001 From: Peter Kotula Date: Sat, 4 Nov 2023 20:14:32 +0100 Subject: [PATCH] json logger - null value for json attribute if value result is empty --- .../scala/zio/logging/JsonLogFormatSpec.scala | 28 +++++++++++++++++++ .../zio/logging/internal/LogAppender.scala | 4 +++ 2 files changed, 32 insertions(+) diff --git a/core/jvm/src/test/scala/zio/logging/JsonLogFormatSpec.scala b/core/jvm/src/test/scala/zio/logging/JsonLogFormatSpec.scala index 2ac697ada..1e68081bf 100644 --- a/core/jvm/src/test/scala/zio/logging/JsonLogFormatSpec.scala +++ b/core/jvm/src/test/scala/zio/logging/JsonLogFormatSpec.scala @@ -298,6 +298,34 @@ object JsonLogFormatSpec extends ZIOSpecDefault { val annEscaped = JsonEscape(nonJsonAnnotation) assertTrue(result == s"""{"json_annotation":$jsonAnnotation,"nonjson_annotation":"$annEscaped"}""") } + }, + test("labeled empty spans") { + val format = label("spans", spans) + val result = format.toJsonLogger( + Trace.empty, + FiberId.None, + LogLevel.Info, + () => "", + Cause.empty, + FiberRefs.empty, + Nil, + Map.empty + ) + assertTrue(result == s"""{"spans":null}""") + }, + test("nested labeled empty spans and annotations") { + val format = label("data", label("line", line) + label("spans", spans) + label("annotations", allAnnotations)) + val result = format.toJsonLogger( + Trace.empty, + FiberId.None, + LogLevel.Info, + () => "line", + Cause.empty, + FiberRefs.empty, + Nil, + Map.empty + ) + assertTrue(result == s"""{"data":{"line":"line","spans":null,"annotations":null}}""") } ) ) diff --git a/core/shared/src/main/scala/zio/logging/internal/LogAppender.scala b/core/shared/src/main/scala/zio/logging/internal/LogAppender.scala index a1d13cc0d..0d271cac4 100644 --- a/core/shared/src/main/scala/zio/logging/internal/LogAppender.scala +++ b/core/shared/src/main/scala/zio/logging/internal/LogAppender.scala @@ -231,6 +231,10 @@ private[logging] object LogAppender { result.append("}") } + if (result.isEmpty) { + result.append("null") + } + stack.pop() result }