Skip to content

Commit

Permalink
Merge pull request #293 from profunktor/feature/logging-stdout-docs
Browse files Browse the repository at this point in the history
Stdout logging + docs
  • Loading branch information
gvolpe authored May 14, 2020
2 parents ac44698 + 4330399 commit 2270f15
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Redis client built on top of [Cats Effect](https://typelevel.org/cats-effect/),
import cats.effect._
import cats.implicits._
import dev.profunktor.redis4cats.Redis
import dev.profunktor.redis4cats.effect.Log.noop
import dev.profunktor.redis4cats.effect.Log.Stdout._

object QuickStart extends IOApp {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,26 @@

package dev.profunktor.redis4cats.effect

import cats.effect.IO
import cats.Applicative
import cats.effect.Sync

/**
* Typeclass used for internal logging such as acquiring and releasing connections.
*
* You should provide an instance. It is recommended to use `log4cats`.
* It is recommended to use `log4cats` for production usage but if you do not want
* the extra dependency, you can opt to use either of the simple instances provided.
*
* If you don't need logging at all, you can use [[Log.NoOp]]
*
* {{{
* import dev.profunktor.redis4cats.effect.Log.NoOp._
* }}}
*
* If you need simple logging to STDOUT for quick debugging, you can use [[Log.Stdout]]
*
* {{{
* import dev.profunktor.redis4cats.effect.Log.Stdout._
* }}}
* */
trait Log[F[_]] {
def info(msg: => String): F[Unit]
Expand All @@ -31,9 +45,22 @@ trait Log[F[_]] {
object Log {
def apply[F[_]](implicit ev: Log[F]): Log[F] = ev

implicit object noop extends Log[IO] {
def info(msg: => String): IO[Unit] = IO.unit
def error(msg: => String): IO[Unit] = IO.unit
object NoOp {
implicit def instance[F[_]: Applicative]: Log[F] =
new Log[F] {
def info(msg: => String): F[Unit] = F.unit
def error(msg: => String): F[Unit] = F.unit
}
}

object Stdout {
implicit def instance[F[_]: Sync]: Log[F] =
new Log[F] {
def info(msg: => String): F[Unit] =
F.delay(Console.out.println(msg))
def error(msg: => String): F[Unit] =
F.delay(Console.err.println(msg))
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import cats.effect._
import cats.implicits._
import dev.profunktor.redis4cats.connection._
import dev.profunktor.redis4cats.data.RedisCodec
import dev.profunktor.redis4cats.effect.Log.noop
import dev.profunktor.redis4cats.effect.Log.NoOp._
import munit.FunSuite
import scala.concurrent.{ Await, ExecutionContext, Future }
import scala.concurrent.duration.Duration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import java.time.Instant

import cats.effect._
import cats.implicits._
import dev.profunktor.redis4cats.effect.Log.noop
import dev.profunktor.redis4cats.effect.Log.NoOp._
import dev.profunktor.redis4cats.effects._
import dev.profunktor.redis4cats.hlist._
import dev.profunktor.redis4cats.pipeline.RedisPipeline
Expand Down
18 changes: 17 additions & 1 deletion site/docs/effects/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,26 @@ def apply[F[_]](uri: RedisURI): Resource[F, RedisClient]

### Logger

In order to create a client and/or connection you must provide a `Log` instance that the library uses for internal logging. You could either create your own or use `log4cats` (recommended). `redis4cats` can derive an instance of `Log[F]` if there is an instance of `Logger[F]` in scope, just need to add the extra dependency `redis4cats-log4cats` and `import dev.profunktor.redis4cats.log4cats._`.
In order to create a client and/or connection you must provide a `Log` instance that the library uses for internal logging. You could either use `log4cats` (recommended), one of the simpler instances such as `NoOp` and `Stdout`, or roll your own. `redis4cats` can derive an instance of `Log[F]` if there is an instance of `Logger[F]` in scope, just need to add the extra dependency `redis4cats-log4cats` and `import dev.profunktor.redis4cats.log4cats._`.

Take a look at the [examples](https://github.com/profunktor/redis4cats/blob/master/modules/examples/src/main/scala/dev/profunktor/redis4cats/LoggerIOApp.scala) to find out more.

#### Disable logging

If you don't need logging at all, use the following import wherever a `Log` instance is required:

```scala
// Available for any `Applicative[F]`
import dev.profunktor.redis4cats.effect.Log.NoOp._
```

If you need simple logging to STDOUT for quick debugging, you can use the following one:

```scala
// Available for any `Sync[F]`
import dev.profunktor.redis4cats.effect.Log.Stdout._
```

### Establishing connection

Here's an example of acquiring a client and a connection to the `Strings API`:
Expand Down
2 changes: 1 addition & 1 deletion site/docs/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ position: 1
import cats.effect._
import cats.implicits._
import dev.profunktor.redis4cats.Redis
import dev.profunktor.redis4cats.effect.Log.noop // disable logging
import dev.profunktor.redis4cats.effect.Log.Stdout._

object QuickStart extends IOApp {

Expand Down

0 comments on commit 2270f15

Please sign in to comment.