Skip to content

Commit

Permalink
Updated json-circe support (Resolves #562)
Browse files Browse the repository at this point in the history
  • Loading branch information
darkfrog26 committed Mar 19, 2024
1 parent 7c6781a commit 07e6252
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 30 deletions.
7 changes: 3 additions & 4 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ val allScalaVersions = List(scala213, scala212, scala3)

name := "scribe"
ThisBuild / organization := "com.outr"
ThisBuild / version := "3.13.0"
ThisBuild / version := "3.13.1-SNAPSHOT"
ThisBuild / scalaVersion := scala213
ThisBuild / scalacOptions ++= Seq("-unchecked", "-deprecation")
ThisBuild / javacOptions ++= Seq("-source", "1.8", "-target", "1.8")
Expand Down Expand Up @@ -205,9 +205,8 @@ lazy val jsonCirce = crossProject(JSPlatform, JVMPlatform)
name := "scribe-json-circe",
libraryDependencies ++= Seq(
"io.circe" %% "circe-core" % circeVersion,
"io.circe" %% "circe-parser" % circeVersion % Test,
"io.circe" %% "circe-generic" % circeVersion % Test,
"io.circe" %% "circe-parser" % circeVersion % Test,
"io.circe" %% "circe-parser" % circeVersion,
"io.circe" %% "circe-generic" % circeVersion,
"org.scalatest" %% "scalatest" % scalaTestVersion % Test
),
crossScalaVersions := allScalaVersions
Expand Down
Original file line number Diff line number Diff line change
@@ -1,42 +1,64 @@
package scribe.json

import io.circe.Json.Null
import io.circe.syntax.EncoderOps
import io.circe.{Json, JsonObject}
import perfolation.long2Implicits
import scribe.{LogRecord, lineSeparator}
import scribe.mdc.MDC
import scribe.message.Message
import scribe.throwable.Trace

import io.circe.generic.auto._

import java.time.{Instant, OffsetDateTime, ZoneId}
object ScribeCirceJsonSupport extends ScribeJsonSupport[Json] {
def json2String(json: Json): String = json.noSpaces

def logRecord2Json(record: LogRecord): Json = {
val traces = record.messages
.collect { case message: Message[_] if message.value.isInstanceOf[Throwable] => message }
.map(_.logOutput.plainText)
val messages = record.messages
.collect { case message: Message[_] if !message.value.isInstanceOf[Throwable] => message }
.map(_.logOutput.plainText)
val timestamp = OffsetDateTime.ofInstant(Instant.ofEpochMilli(record.timeStamp), ZoneId.of("UTC"))
val service = MDC.get("service").map(_.toString)
override def logRecord2Json(record: LogRecord): Json = {
val l = record.timeStamp
val traces = record.messages.map(_.value).collect {
case trace: Trace => trace
} match {
case Nil => Null
case t :: Nil => t.asJson
case list => list.asJson
}
val messages = record.messages.collect {
case message: Message[_] if !message.value.isInstanceOf[Throwable] => message.value match {
case json: Json => json
case _ => message.logOutput.plainText.asJson
}
} match {
case Nil => Null
case m :: Nil => m
case list => list.toVector.asJson
}
val data = MDC.map ++ record.data
JsonObject(
"messages" -> messages.asJson,
"service" -> service.asJson,
"level" -> record.level.name.asJson,
"value" -> record.levelValue.asJson,
"levelValue" -> record.levelValue.asJson,
"message" -> messages,
"fileName" -> record.fileName.asJson,
"className" -> record.className.asJson,
"methodName" -> record.methodName.asJson,
"line" -> record.line.asJson,
"thread" -> record.thread.getName.asJson,
"@timestamp" -> timestamp.asJson,
"stack_trace" -> (if (traces.isEmpty) Json.Null else traces.mkString(lineSeparator).asJson),
"mdc" -> JsonObject.fromMap(MDC.map.map { case (key, function) =>
key -> function().toString.asJson
}).asJson,
"data" -> JsonObject.fromMap(record.data.map { case (key, function) =>
key -> function().toString.asJson
}).asJson
).asJson
}
"methodName" -> record.methodName.map(_.asJson).getOrElse(Null),
"line" -> record.line.map(_.asJson).getOrElse(Null),
"column" -> record.column.map(_.asJson).getOrElse(Null),
"data" -> data.toList.map {
case (key, value) => value() match {
case json: Json => key -> json
case any => key -> any.toString.asJson
}
}.asJson,
"mdc" -> MDC.map.map {
case (key, value) => value() match {
case json: Json => key -> json
case any => key -> any.toString.asJson
}
}.asJson,
"trace" -> traces,
"timeStamp" -> l.asJson,
"date" -> l.t.F.asJson,
"time" -> s"${l.t.T}.${l.t.L}${l.t.z}".asJson
)
}.asJson
}

0 comments on commit 07e6252

Please sign in to comment.