https://github.com/scalalandio/log4effect
Cats Effect syntax for logging
https://github.com/scalalandio/log4effect
cats-effect interpolation logging
Last synced: about 1 month ago
JSON representation
Cats Effect syntax for logging
- Host: GitHub
- URL: https://github.com/scalalandio/log4effect
- Owner: scalalandio
- License: apache-2.0
- Created: 2019-09-25T07:32:27.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-10-30T15:31:51.000Z (over 5 years ago)
- Last Synced: 2025-01-07T18:43:14.965Z (over 1 year ago)
- Topics: cats-effect, interpolation, logging
- Language: Scala
- Size: 138 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# log4effect
[](https://travis-ci.org/scalalandio/log4effect)
[](http://search.maven.org/#search%7Cga%7C1%7Clog4effect)
[](http://www.apache.org/licenses/LICENSE-2.0.txt)
If you missed some syntax for logging with Cats Effect, then here it is.
## Getting started
Library is available for Scala 2.11, 2.12, 2.13.
Add it with:
```scala
libraryDependencies += "io.scalaland" %% "log4effect" % log4EffectVersion
```
## Usage
Logging is done using `Logged` type class:
```scala
import cats.effect.Sync
import io.scalaland.log4effect.Logged
def operation[F[_]: Sync: Logged] = for {
a <- Sync[F].defer(1 + 1)
b <- Sync[F].defer(2 + 2)
_ <- Logged[F].info(s"a = $a b=$b")
} yield a -> b
```
If you want you can use interpolator syntax - it assumes that there is
`cats.Show` instance for any value that you use in it.
```scala
import cats.implicits._
import io.scalaland.log4effect.Logged
import io.scalaland.log4effect.syntax._
val i = 1
val d = 1.0
val s = "test"
def logs[F[_]: Monad: Logged] = for {
_ <- trace"$i $d $s"[F]
_ <- trace"$i $d $s".withEx[F](new Exception("with ex"))
_ <- debug"$i $d $s"[F]
_ <- debug"$i $d $s".withEx[F](new Exception("with ex"))
_ <- info"$i $d $s"[F]
_ <- info"$i $d $s".withEx[F](new Exception("with ex"))
_ <- warn"$i $d $s"[F]
_ <- warn"$i $d $s".withEx[F](new Exception("with ex"))
_ <- error"$i $d $s"[F]
_ <- error"$i $d $s".withEx[F](new Exception("with ex"))
} yield ()
```
If there is no `cats.Show` you'd have to call `.toString` explicitly.