Created
January 23, 2022 16:24
-
-
Save searler/52c7967e5c39fd0729f5241174582a20 to your computer and use it in GitHub Desktop.
Adding custom ZLogger to capture unhandled defects
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
// 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