Skip to content

Instantly share code, notes, and snippets.

@searler
Created January 23, 2022 16:24
Show Gist options
  • Save searler/52c7967e5c39fd0729f5241174582a20 to your computer and use it in GitHub Desktop.
Save searler/52c7967e5c39fd0729f5241174582a20 to your computer and use it in GitHub Desktop.
Adding custom ZLogger to capture unhandled defects
// https://github.com/zio/zio/issues/6278
import zio.{Cause, FiberId, FiberRef, LogLevel, LogSpan, RuntimeConfigAspect, ZIO, ZIOAppDefault, ZLogger, ZTraceElement}
object Example extends ZIOAppDefault {
val specialLogger: ZLogger[Cause[Any], Unit] =
(
trace: ZTraceElement,
fiberId: FiberId,
level: LogLevel,
message: () => Cause[Any],
context: Map[FiberRef.Runtime[_], AnyRef],
spans: List[LogSpan],
location: ZTraceElement
) => {
try {
System.err.println(message().prettyPrint)
()
} catch {
case t: Throwable => t.printStackTrace()
}
}
override def hook = RuntimeConfigAspect.addLogger(specialLogger)
val run =
for {
f <- ZIO.succeed(throw new Exception("bang")).fork
_ <- f.await
} yield ()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment