From b1ac8aa2136b1d2702b62b4b34a190e20e69317f Mon Sep 17 00:00:00 2001 From: Matthias Langer Date: Sun, 11 Sep 2022 15:44:58 +0200 Subject: [PATCH] Update README --- README.MD | 131 +++++++++--------- .../mlangc/slf4zio/ReadmeExamplesTest.scala | 15 +- 2 files changed, 71 insertions(+), 75 deletions(-) diff --git a/README.MD b/README.MD index e66c559..e18663e 100644 --- a/README.MD +++ b/README.MD @@ -15,33 +15,33 @@ my personal preference: ### 1. Using the LoggingSupport Convenience Trait ````scala -import zio._ -import zio.random -import zio.random.Random -import zio.clock.Clock -import zio.duration.durationInt import com.github.mlangc.slf4zio.api._ +import zio._ object SomeObject extends LoggingSupport { - def doStuff: RIO[Random with Clock, Unit] = { + def doStuff: Task[Unit] = { for { - _ <- logger.warnIO("What the heck") - _ <- ZIO.ifM(random.nextBoolean)( + _ <- logger.warnIO("What the heck") + _ <- ZIO.ifZIO(Random.nextBoolean)( logger.infoIO("Uff, that was close"), logger.errorIO("Game over", new IllegalStateException("This is the end")) ) - _ <- Task { + _ <- ZIO.attempt { // logger is just a plain SLF4J logger; you can therefore use it from // effectful code directly: logger.trace("Wink wink nudge nudge") } - _ <- ZIO.sleep(8.millis).as(23).perfLog( - // See below for more examples with `LogSpec` - LogSpec.onSucceed[Int]((d, i) => debug"Finally done with $i after ${d.render}") - .withThreshold(5.millis) - ) + _ <- ZIO + .sleep(8.millis) + .as(23) + .perfLog( + // See below for more examples with `LogSpec` + LogSpec + .onSucceed[Int]((d, i) => debug"Finally done with $i after ${d.render}") + .withThreshold(5.millis) + ) } yield () } } @@ -63,35 +63,30 @@ and not care too much about this subtlety. ```scala import com.github.mlangc.slf4zio.api._ -import zio.duration.durationInt -import zio.clock.Clock -import zio.RIO -import zio.ZIO -import zio.Task +import zio._ -val effect: RIO[Clock, Unit] = { - // ... - class SomeClass +val effect: Task[Unit] = { + // ... + class SomeClass + // ... + for { + logger <- makeLogger[SomeClass] + _ <- logger.debugIO("Debug me tender") // ... - for { - logger <- makeLogger[SomeClass] - _ <- logger.debugIO("Debug me tender") + _ <- ZIO.attempt { + // Note that makeLogger just returns a plain SLF4J logger; you can therefore use it from + // effectful code directly: + logger.info("Don't be shy") // ... - _ <- Task { - // Note that makeLogger just returns a plain SLF4J logger; you can therefore use it from - // effectful code directly: - logger.info("Don't be shy") - // ... - logger.warn("Please take me home") - // ... - } + logger.warn("Please take me home") // ... - // Generate highly configurable performance logs with ease: - _ <- logger.perfLogZIO(ZIO.sleep(10.millis)) { - LogSpec.onSucceed(d => info"Feeling relaxed after sleeping ${d.render}") ++ - LogSpec.onTermination((d, c) => error"Woke up after ${d.render}: ${c.prettyPrint}") - } - } yield () + } + // ... + _ <- logger.perfLogZIO(ZIO.sleep(10.millis))( + // See below for more examples with `LogSpec` + LogSpec.onSucceed(d => info"Feeling relaxed after sleeping ${d.render}") + ) + } yield () } ``` @@ -99,39 +94,36 @@ val effect: RIO[Clock, Unit] = { ```scala import com.github.mlangc.slf4zio.api._ -import zio.RIO -import zio.ZIO -import zio.Task -import zio.clock.Clock - -val effect: RIO[Logging with Clock, Unit] = -for { - _ <- logging.warnIO("Surprise, surprise") - plainLogger <- logging.logger - _ <- Task { - plainLogger.debug("Shhh...") - plainLogger.warn("The devil always comes in disguise") - } - _ <- logging.traceIO("...") - getNumber = ZIO.succeed(42) - _ <- getNumber.perfLogZ(LogSpec.onSucceed(d => debug"Got number after ${d.render}")) -} yield () +import zio._ + +val effect: RIO[Logging, Unit] = + for { + _ <- logging.warnIO("Surprise, surprise") + plainLogger <- logging.logger + _ <- ZIO.attempt { + plainLogger.debug("Shhh...") + plainLogger.warn("The devil always comes in disguise") + } + _ <- logging.traceIO("...") + getNumber = ZIO.succeed(42) + // See below for more examples with `LogSpec` + _ <- getNumber.perfLogZ(LogSpec.onSucceed(d => debug"Got number after ${d.render}")) + } yield () ``` ### Using Markers ```scala import com.github.mlangc.slf4zio.api._ -import zio.{RIO, Task} -import zio.clock.Clock +import zio._ -val effect: RIO[Logging with Clock, Unit] = +val effect: RIO[Logging, Unit] = for { marker <- getMarker("[MARKER]") _ <- logging.infoIO(marker, "Here we are") logger <- logging.logger _ <- logger.debugIO(marker, "Wat?") - _ <- Task { + _ <- ZIO.attempt { logger.warn(marker, "Don't worry") } } yield () @@ -145,20 +137,29 @@ ones, utilizing the underlying monoidial structure: ```scala import com.github.mlangc.slf4zio.api._ +import zio._ // Simple specs can be combined using the `++` to obtain more complex specs val logSpec1: LogSpec[Throwable, Int] = - LogSpec.onSucceed[Int]((d, a) => info"Succeeded after ${d.render} with $a") ++ - LogSpec.onError[Throwable]((d, th) => error"Failed after ${d.render} with $th") ++ - LogSpec.onTermination((d, c) => error"Fatal failure after ${d.render}: ${c.prettyPrint}") + LogSpec.onSucceed[Int]((d, a) => info"Succeeded after ${d.render} with $a") ++ + LogSpec.onError[Throwable]((d, th) => error"Failed after ${d.render} with $th") ++ + LogSpec.onTermination((d, c) => error"Fatal failure after ${d.render}: ${c.prettyPrint}") // A threshold can be applied to a LogSpec. Nothing will be logged, unless the threshold is exceeded. val logSpec2: LogSpec[Any, Any] = - LogSpec.onSucceed(d => warn"Operation took ${d.render}") - .withThreshold(1.milli) + LogSpec + .onSucceed(d => warn"Operation took ${d.render}") + .withThreshold(1.milli) // Will behave like logSpec1 and eventually log a warning as specified in logSpec2 val logSpec3: LogSpec[Throwable, Int] = logSpec1 ++ logSpec2 + +val effect: ZIO[Logging, Nothing, Unit] = for { + _ <- ZIO.sleep(5.micros).perfLogZ(LogSpec.onSucceed(d => debug"Done after ${d.render}")) + _ <- ZIO.sleep(1.milli).as(42).perfLogZ(logSpec1) + _ <- ZIO.sleep(2.milli).perfLogZ(logSpec2) + _ <- ZIO.sleep(3.milli).as(23).perfLogZ(logSpec3) +} yield () ``` ### Using MDC convenience APIs diff --git a/src/test/scala/com/github/mlangc/slf4zio/ReadmeExamplesTest.scala b/src/test/scala/com/github/mlangc/slf4zio/ReadmeExamplesTest.scala index ec7216f..fe95883 100644 --- a/src/test/scala/com/github/mlangc/slf4zio/ReadmeExamplesTest.scala +++ b/src/test/scala/com/github/mlangc/slf4zio/ReadmeExamplesTest.scala @@ -1,19 +1,14 @@ package com.github.mlangc.slf4zio import com.github.mlangc.slf4zio.api.Logging -import zio.duration2DurationOps import zio.test._ import zio.test.Assertion._ -import zio.test.TestAspect -import zio.test.ZIOSpecDefault -import zio.Task -import zio.ZIO object ReadmeExamplesTest extends ZIOSpecDefault { def spec = suite("ReadmeExamplesTest")( test("creating loggers as needed") { import com.github.mlangc.slf4zio.api._ - import zio.{ZIO, durationInt} + import zio._ val effect: Task[Unit] = { // ... @@ -43,7 +38,7 @@ object ReadmeExamplesTest extends ZIOSpecDefault { }, test("Using the convenience trait") { import com.github.mlangc.slf4zio.api._ - import zio.{Random, ZIO, durationInt} + import zio._ object SomeObject extends LoggingSupport { def doStuff: Task[Unit] = { @@ -77,7 +72,7 @@ object ReadmeExamplesTest extends ZIOSpecDefault { }, test("Using the service") { import com.github.mlangc.slf4zio.api._ - import zio.{RIO, ZIO} + import zio._ val effect: RIO[Logging, Unit] = for { @@ -97,7 +92,7 @@ object ReadmeExamplesTest extends ZIOSpecDefault { }, test("Performance Logging - Using the Logging Service") { import com.github.mlangc.slf4zio.api._ - import zio.{ZIO, durationInt} + import zio._ // Simple specs can be combined using the `++` to obtain more complex specs val logSpec1: LogSpec[Throwable, Int] = @@ -125,7 +120,7 @@ object ReadmeExamplesTest extends ZIOSpecDefault { }, test("Working with Markers") { import com.github.mlangc.slf4zio.api._ - import zio.RIO + import zio._ val effect: RIO[Logging, Unit] = for {