-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated json-circe support (Resolves #562)
- Loading branch information
1 parent
7c6781a
commit 07e6252
Showing
2 changed files
with
51 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 48 additions & 26 deletions
74
jsonCirce/shared/src/main/scala/scribe/json/ScribeCirceJsonSupport.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |