Skip to content

Commit

Permalink
Update README
Browse files Browse the repository at this point in the history
  • Loading branch information
mlangc committed Sep 11, 2022
1 parent 8d66934 commit b1ac8aa
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 75 deletions.
131 changes: 66 additions & 65 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()
}
}
Expand All @@ -63,75 +63,67 @@ 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 ()
}
```

### 3. Using the Logging Service

```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 ()
Expand All @@ -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
Expand Down
15 changes: 5 additions & 10 deletions src/test/scala/com/github/mlangc/slf4zio/ReadmeExamplesTest.scala
Original file line number Diff line number Diff line change
@@ -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] = {
// ...
Expand Down Expand Up @@ -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] = {
Expand Down Expand Up @@ -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 {
Expand All @@ -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] =
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit b1ac8aa

Please sign in to comment.