Skip to content

Commit

Permalink
json logger - null value for json attribute if value result is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
justcoon committed Nov 4, 2023
1 parent b118477 commit 94c1427
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
28 changes: 28 additions & 0 deletions core/jvm/src/test/scala/zio/logging/JsonLogFormatSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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}}""")
}
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,10 @@ private[logging] object LogAppender {
result.append("}")
}

if (result.isEmpty) {
result.append("null")
}

stack.pop()
result
}
Expand Down

0 comments on commit 94c1427

Please sign in to comment.