From 4330399e866babdad67d55df5aee0db5b85e6ca7 Mon Sep 17 00:00:00 2001 From: Gabriel Volpe Date: Thu, 14 May 2020 13:09:24 +0200 Subject: [PATCH] Stdout logging + docs --- README.md | 2 +- .../profunktor/redis4cats/effect/Log.scala | 37 ++++++++++++++++--- .../redis4cats/Redis4CatsFunSuite.scala | 2 +- .../profunktor/redis4cats/TestScenarios.scala | 2 +- site/docs/effects/index.md | 18 ++++++++- site/docs/quickstart.md | 2 +- 6 files changed, 53 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 33f9fac6..7d30e3da 100644 --- a/README.md +++ b/README.md @@ -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 { diff --git a/modules/core/src/main/scala/dev/profunktor/redis4cats/effect/Log.scala b/modules/core/src/main/scala/dev/profunktor/redis4cats/effect/Log.scala index c0105c68..743460dc 100644 --- a/modules/core/src/main/scala/dev/profunktor/redis4cats/effect/Log.scala +++ b/modules/core/src/main/scala/dev/profunktor/redis4cats/effect/Log.scala @@ -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] @@ -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)) + } } } diff --git a/modules/tests/src/test/scala/dev/profunktor/redis4cats/Redis4CatsFunSuite.scala b/modules/tests/src/test/scala/dev/profunktor/redis4cats/Redis4CatsFunSuite.scala index e52de5d6..f367e3ad 100644 --- a/modules/tests/src/test/scala/dev/profunktor/redis4cats/Redis4CatsFunSuite.scala +++ b/modules/tests/src/test/scala/dev/profunktor/redis4cats/Redis4CatsFunSuite.scala @@ -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 diff --git a/modules/tests/src/test/scala/dev/profunktor/redis4cats/TestScenarios.scala b/modules/tests/src/test/scala/dev/profunktor/redis4cats/TestScenarios.scala index c7bef1d4..8ef41870 100644 --- a/modules/tests/src/test/scala/dev/profunktor/redis4cats/TestScenarios.scala +++ b/modules/tests/src/test/scala/dev/profunktor/redis4cats/TestScenarios.scala @@ -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 diff --git a/site/docs/effects/index.md b/site/docs/effects/index.md index 5281643e..cd008da0 100644 --- a/site/docs/effects/index.md +++ b/site/docs/effects/index.md @@ -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`: diff --git a/site/docs/quickstart.md b/site/docs/quickstart.md index 01f20119..444e720e 100644 --- a/site/docs/quickstart.md +++ b/site/docs/quickstart.md @@ -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 {