Skip to content

Commit

Permalink
after review
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Malyshev committed Nov 18, 2022
1 parent e1b4bd7 commit 86303db
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ object ZMakeSimpleExample extends ZIOAppDefault {
for {
id <- Random.nextIntBetween(1000, 4000)
_ <- ZIO.serviceWithZIO[SimpleContext](_.set(id.toHexString))
_ <- ZIO.serviceWithZIO[FooService](_.foo(i))
_ <- ZIO.serviceWithZIO[FooService](_.foo(i)) // each task prints its own requestId set above
} yield ()
)

Expand All @@ -20,7 +20,7 @@ object ZMakeSimpleExample extends ZIOAppDefault {
.collectAllParDiscard(tasks)
.provide(
FooService.layer,
ZLogging.Make.layerPlainWithContext,
SimpleContext.layer
SimpleContext.layer, // ULayer[SimpleContext]
ZLogging.Make.layerPlainWithContext, // requires ContextProvider, but easily finds its implementation — SimpleContext
)
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package tofu.example.logging.impl

import tofu.logging.{Loggable, LoggedValue}
import tofu.logging.zlogs.ContextProvider
import tofu.logging.Loggable
import tofu.logging.zlogs.ValueContextProvider
import zio.{FiberRef, UIO, ULayer, ZLayer}

class SimpleContext(ref: FiberRef[String]) extends ContextProvider {
private implicit val requestIdLoggable: Loggable[String] =
Loggable.stringValue.named("requestId")
class SimpleContext(ref: FiberRef[String])
extends ValueContextProvider[String]()(Loggable.stringValue.named("requestId")) {

override def getCtx: UIO[LoggedValue] = ref.get.map(x => x)
override def getA: UIO[String] = ref.get

def set(requestId: String): UIO[Unit] =
ref.set(requestId)
Expand All @@ -17,7 +16,7 @@ class SimpleContext(ref: FiberRef[String]) extends ContextProvider {
object SimpleContext {
val layer: ULayer[SimpleContext] = ZLayer.scoped(
FiberRef
.make[String]("")
.make[String]("undefined_request_id")
.map(new SimpleContext(_))
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import tofu.logging.zlogs.ZLogging
import tofu.logging.{Loggable, LoggedValue, Logging}
import zio._

class ZContextLogging[R, Ctx: Loggable](logger: => Logger, ctxLog: URIO[R, Ctx]) extends ZLogging[R] {
private[logging] class ZContextLogging[R, Ctx: Loggable](logger: => Logger, ctxLog: URIO[R, Ctx]) extends ZLogging[R] {
override def write(level: Logging.Level, message: String, values: LoggedValue*): URIO[R, Unit] =
ctxLog.flatMap { ctx =>
ZIO.succeed(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package tofu.logging.zlogs

import tofu.logging.LoggedValue
import tofu.logging.{Loggable, LoggedValue}
import zio.UIO

trait ContextProvider {
def getCtx: UIO[LoggedValue]
}

abstract class ValueContextProvider[A](implicit L: Loggable[A]) extends ContextProvider {
protected def getA: UIO[A]

override def getCtx: UIO[LoggedValue] = getA.map(x => x)
}

0 comments on commit 86303db

Please sign in to comment.