-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement zio2 tofu logging like zio1, write example Co-authored-by: Ivan Malyshev <[email protected]>
- Loading branch information
Showing
13 changed files
with
308 additions
and
5 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
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
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<configuration scan="true" scanPeriod="10 minutes" > | ||
<appender name="structured" class="ch.qos.logback.core.ConsoleAppender" packagingData="true"> | ||
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder"> | ||
<charset>UTF-8</charset> | ||
<layout class="tofu.logging.ELKLayout"/> | ||
</encoder> | ||
</appender> | ||
|
||
<root level="DEBUG"> | ||
<appender-ref ref="structured"/> | ||
</root> | ||
</configuration> |
26 changes: 26 additions & 0 deletions
26
examples-zio2/src/main/scala/tofu/example/logging/ZMakeSimpleExample.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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package tofu.example.logging | ||
|
||
import tofu.example.logging.impl.{FooService, SimpleContext} | ||
import tofu.logging.zlogs.ZLogging | ||
import zio._ | ||
|
||
object ZMakeSimpleExample extends ZIOAppDefault { | ||
|
||
val tasks = (1 to 3) | ||
.map(i => | ||
for { | ||
id <- Random.nextIntBetween(1000, 4000) | ||
_ <- ZIO.serviceWithZIO[SimpleContext](_.set(id.toHexString)) | ||
_ <- ZIO.serviceWithZIO[FooService](_.foo(i)) // each task prints its own requestId set above | ||
} yield () | ||
) | ||
|
||
override def run = | ||
ZIO | ||
.collectAllParDiscard(tasks) | ||
.provide( | ||
FooService.layer, | ||
SimpleContext.layer, // ULayer[SimpleContext] | ||
ZLogging.Make.layerPlainWithContext, // requires ContextProvider, but easily finds its implementation — SimpleContext | ||
) | ||
} |
21 changes: 21 additions & 0 deletions
21
examples-zio2/src/main/scala/tofu/example/logging/impl/FooService.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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package tofu.example.logging.impl | ||
|
||
import tofu.logging.zlogs.{ULogging, ZLogging} | ||
import zio._ | ||
|
||
class FooService( | ||
logs: ZLogging.Make | ||
) { | ||
private val logger: ULogging = logs.forService[FooService] | ||
|
||
def foo(i: Int): UIO[Int] = | ||
for { | ||
_ <- logger.debug("Starting Foo! i={}", i) | ||
_ <- Clock.sleep(i.seconds) | ||
_ <- logger.info("Foo completed!") | ||
} yield i | ||
} | ||
|
||
object FooService { | ||
val layer: URLayer[ZLogging.Make, FooService] = ZLayer.fromFunction(new FooService(_)) | ||
} |
22 changes: 22 additions & 0 deletions
22
examples-zio2/src/main/scala/tofu/example/logging/impl/SimpleContext.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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package tofu.example.logging.impl | ||
|
||
import tofu.logging.Loggable | ||
import tofu.logging.zlogs.ValueContextProvider | ||
import zio.{FiberRef, UIO, ULayer, ZLayer} | ||
|
||
class SimpleContext(ref: FiberRef[String]) | ||
extends ValueContextProvider[String]()(Loggable.stringValue.named("requestId")) { | ||
|
||
override def getA: UIO[String] = ref.get | ||
|
||
def set(requestId: String): UIO[Unit] = | ||
ref.set(requestId) | ||
} | ||
|
||
object SimpleContext { | ||
val layer: ULayer[SimpleContext] = ZLayer.scoped( | ||
FiberRef | ||
.make[String]("undefined_request_id") | ||
.map(new SimpleContext(_)) | ||
) | ||
} |
34 changes: 34 additions & 0 deletions
34
modules/zio2/logging/src/main/scala/tofu/logging/impl/ZContextLogging.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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package tofu.logging.impl | ||
|
||
import org.slf4j.{Logger, Marker} | ||
import tofu.logging.zlogs.ZLogging | ||
import tofu.logging.{Loggable, LoggedValue, Logging} | ||
import zio._ | ||
|
||
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( | ||
ZUniversalContextLogging.write(level, logger, message, ctx, values, Nil) | ||
) | ||
} | ||
|
||
override def writeMarker(level: Logging.Level, message: String, marker: Marker, values: LoggedValue*): URIO[R, Unit] = | ||
ctxLog.flatMap { ctx => | ||
ZIO.succeed( | ||
ZUniversalContextLogging.write(level, logger, message, ctx, values, marker :: Nil) | ||
) | ||
} | ||
|
||
override def writeCause( | ||
level: Logging.Level, | ||
message: String, | ||
cause: Throwable, | ||
values: LoggedValue* | ||
): URIO[R, Unit] = | ||
ctxLog.flatMap { ctx => | ||
ZIO.succeed( | ||
ZUniversalContextLogging.writeCause(level, logger, cause, message, ctx, values, Nil) | ||
) | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
modules/zio2/logging/src/main/scala/tofu/logging/impl/ZUniversalContextLogging.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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package tofu.logging.impl | ||
|
||
import org.slf4j.{Logger, LoggerFactory, Marker} | ||
import tofu.logging._ | ||
import zio.URIO | ||
|
||
class ZUniversalContextLogging[R, Ctx: Loggable](name: String, ctxLog: URIO[R, Ctx]) | ||
extends ZContextLogging[R, Ctx](LoggerFactory.getLogger(name), ctxLog) | ||
|
||
object ZUniversalContextLogging { | ||
|
||
private[logging] def write( | ||
level: Logging.Level, | ||
logger: Logger, | ||
message: => String, | ||
ctx: LoggedValue, | ||
values: Seq[LoggedValue], | ||
markers: List[Marker] = Nil | ||
): Unit = { | ||
if (UniversalLogging.enabled(level, logger)) | ||
UniversalLogging.writeMarker( | ||
level = level, | ||
logger = logger, | ||
marker = ContextMarker(ctx, markers), | ||
message = message, | ||
values = values | ||
) | ||
} | ||
|
||
private[logging] def writeCause( | ||
level: Logging.Level, | ||
logger: Logger, | ||
cause: Throwable, | ||
message: => String, | ||
ctx: LoggedValue, | ||
values: Seq[LoggedValue], | ||
markers: List[Marker] = Nil | ||
): Unit = { | ||
if (UniversalLogging.enabled(level, logger)) | ||
UniversalLogging.writeMarkerCause( | ||
level = level, | ||
logger = logger, | ||
marker = ContextMarker(ctx, markers), | ||
cause = cause, | ||
message = message, | ||
values = values | ||
) | ||
} | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
modules/zio2/logging/src/main/scala/tofu/logging/zlogs/ContextProvider.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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package tofu.logging.zlogs | ||
|
||
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) | ||
} |
48 changes: 48 additions & 0 deletions
48
modules/zio2/logging/src/main/scala/tofu/logging/zlogs/ZLogging.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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package tofu.logging.zlogs | ||
|
||
import tofu.logging._ | ||
import tofu.logging.impl.ZUniversalContextLogging | ||
import zio._ | ||
|
||
import scala.annotation.unused | ||
|
||
object ZLogging { | ||
|
||
type ZMake[R] = Logging.Make[URIO[R, *]] | ||
|
||
type Make = Logging.Make[UIO] | ||
|
||
def empty[R]: ZLogging[R] = new Logging[URIO[R, *]] { | ||
override def write(level: Logging.Level, message: String, values: LoggedValue*): URIO[R, Unit] = ZIO.unit | ||
} | ||
|
||
/** Unlike [[ZLogs]] layers, these helpers are effect-less. | ||
*/ | ||
object Make { | ||
|
||
/** Creates layer with a helper producing simple [[tofu.logging.Logging]]. | ||
*/ | ||
val layerPlain: ULayer[ZLogging.Make] = ZLayer.succeed(new ZUniversalContextLogging(_, ZIO.unit)) | ||
|
||
/** Creates layer with a helper [[ZMake]] producing logging `ZLogging[R]`. Logging methods will add the context from | ||
* the ZIO environment. | ||
* | ||
* @tparam R | ||
* the context, an environment of the logging methods. | ||
*/ | ||
def layerContextual[R: Loggable](implicit @unused RT: Tag[R]): ULayer[ZLogging.ZMake[R]] = ZLayer.succeed( | ||
new ZUniversalContextLogging(_, ZIO.service[R]) | ||
) | ||
|
||
/** Creates layer with a helper [[Make]] producing plain `ULogging` with encapsulated context. You have to provide | ||
* an implementation of `ContextProvider`. | ||
*/ | ||
def layerPlainWithContext: URLayer[ContextProvider, ZLogging.Make] = | ||
ZLayer( | ||
ZIO.serviceWith[ContextProvider](cp => new ZUniversalContextLogging(_, cp.getCtx)) | ||
) | ||
|
||
val layerEmpty: ULayer[ZLogging.Make] = ZLayer.succeed(_ => empty[Any]) | ||
} | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
modules/zio2/logging/src/main/scala/tofu/logging/zlogs/package.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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package tofu.logging | ||
|
||
import zio.{UIO, URIO} | ||
|
||
package object zlogs { | ||
type ZLogs[R] = Logs[UIO, URIO[R, *]] | ||
type ULogs = Logs[UIO, UIO] | ||
type ZLogging[R] = Logging[URIO[R, *]] | ||
type ULogging = Logging[UIO] | ||
} |
41 changes: 41 additions & 0 deletions
41
modules/zio2/logging/src/main/scala/tofu/logging/zlogs/zlogs.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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package tofu.logging.zlogs | ||
|
||
import org.slf4j.{Logger, LoggerFactory} | ||
import tofu.logging.impl.ZContextLogging | ||
import tofu.logging.{Loggable, Logs} | ||
import zio._ | ||
|
||
import scala.annotation.unused | ||
|
||
object ZLogs { | ||
|
||
private def make[R](f: Logger => ZLogging[R]) = | ||
new Logs[UIO, URIO[R, *]] { | ||
override def byName(name: String): UIO[ZLogging[R]] = | ||
ZIO.succeed(LoggerFactory.getLogger(name)).map(f) | ||
} | ||
|
||
/** Creates layer with a helper producing simple [[tofu.logging.Logging]]. | ||
*/ | ||
def layerPlain: ULayer[ULogs] = | ||
ZLayer.succeed(make(new ZContextLogging(_, ZIO.unit))) | ||
|
||
/** Creates layer with a helper [[ZLogs]] producing logging `ZLogging[R]`. Logging methods will add the context from | ||
* the ZIO environment. | ||
* | ||
* @tparam R | ||
* the context, an environment of the logging methods. | ||
*/ | ||
def layerContextual[R: Loggable](implicit @unused RT: Tag[R]): ULayer[ZLogs[R]] = | ||
ZLayer.succeed(make(new ZContextLogging(_, ZIO.service[R]))) | ||
|
||
/** Creates layer with a helper [[ULogs]] producing plain `ULogging` with encapsulated context. You have to provide an | ||
* implementation of `ContextProvider`. | ||
*/ | ||
val layerPlainWithContext: URLayer[ContextProvider, ULogs] = ZLayer( | ||
ZIO.serviceWith[ContextProvider](cp => make(new ZContextLogging(_, cp.getCtx))) | ||
) | ||
|
||
val layerEmpty: ULayer[ULogs] = ZLayer.succeed(make(_ => ZLogging.empty[Any])) | ||
|
||
} |
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