Skip to content

Commit

Permalink
Updates to JSON support
Browse files Browse the repository at this point in the history
  • Loading branch information
darkfrog26 committed Mar 20, 2024
1 parent 24c93ef commit 1cd7d1b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,24 @@ trait ScribeCirceJsonSupport extends ScribeJsonSupport[Json] {

override def logRecord2Json(record: LogRecord): Json = {
val l = record.timeStamp
def trace2Json(trace: Trace): Json = JsonObject(
"className" -> trace.className.asJson,
"message" -> trace.message.asJson,
"elements" -> trace.elements.map { e =>
JsonObject(
"class" -> e.`class`.asJson,
"fileName" -> e.fileName.asJson,
"method" -> e.method.asJson,
"line" -> e.line.asJson
)
}.asJson,
"cause" -> trace.cause.map(trace2Json).asJson
).asJson
val traces = record.messages.map(_.value).collect {
case trace: Trace => trace
case trace: Trace => trace2Json(trace)
} match {
case Nil => Null
case t :: Nil => t.asJson
case t :: Nil => t
case list => list.asJson
}
val messages = record.messages.collect {
Expand Down
11 changes: 5 additions & 6 deletions jsonCirce/shared/src/test/scala/spec/JsonWriterSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@ class JsonWriterSpec extends AnyWordSpec with Matchers with Inside {
logger.info("Hello, Json!")
cache.output.length should be(1)
inside(parse(cache.output.head.plainText).toOption.flatMap(_.asObject)) {
case Some(json) => {
case Some(json) =>
json("line").flatMap(_.asNumber).flatMap(_.toLong) should be(Some(29))
json("fileName").flatMap(_.asString) should be(Some("JsonWriterSpec.scala"))
json("messages").flatMap(_.as[List[String]].toOption) should be(Some(List("Hello, Json!")))
}
json("message").flatMap(_.asString) should be(Some("Hello, Json!"))
}
}

Expand All @@ -44,10 +43,10 @@ class JsonWriterSpec extends AnyWordSpec with Matchers with Inside {
cache.output.length should be(1)
inside(parse(cache.output.head.plainText).toOption.flatMap(_.asObject)) {
case Some(json) =>
json("@timestamp").flatMap(_.asString) should be(Some("2021-01-02T08:00:00Z"))
json("line").flatMap(_.asNumber).flatMap(_.toLong) should be(Some(43))
json("date").flatMap(_.asString) should be(Some("2021-01-02"))
json("line").flatMap(_.asNumber).flatMap(_.toLong) should be(Some(42))
json("fileName").flatMap(_.asString) should be(Some("JsonWriterSpec.scala"))
json("messages").flatMap(_.as[List[String]].toOption) should be(Some(List("Failure, Json!")))
json("message").flatMap(_.asString) should be(Some("Failure, Json!"))
}
}

Expand Down

0 comments on commit 1cd7d1b

Please sign in to comment.