Skip to content

Commit

Permalink
Relax SLF4J constraints from CE with Timer to Effect with Clock (#171)
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeykolbasov authored Aug 13, 2020
1 parent 4f52f7f commit a91e22c
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 14 deletions.
7 changes: 4 additions & 3 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ libraryDependencies += "com.github.valskalla" %% "odin-slf4j" % "@VERSION@"
```scala mdoc:reset
//package org.slf4j.impl

import cats.effect.{ConcurrentEffect, ContextShift, IO, Timer}
import cats.effect.{ContextShift, Clock, Effect, IO, Timer}
import io.odin._
import io.odin.slf4j.OdinLoggerBinder

Expand All @@ -706,10 +706,11 @@ import scala.concurrent.ExecutionContext
//log line will be recorded right after the call with no suspension
class StaticLoggerBinder extends OdinLoggerBinder[IO] {

val ec: ExecutionContext = scala.concurrent.ExecutionContext.global //or other EC of your choice
val ec: ExecutionContext = scala.concurrent.ExecutionContext.global
implicit val timer: Timer[IO] = IO.timer(ec)
implicit val clock: Clock[IO] = timer.clock
implicit val cs: ContextShift[IO] = IO.contextShift(ec)
implicit val F: ConcurrentEffect[IO] = IO.ioConcurrentEffect
implicit val F: Effect[IO] = IO.ioEffect

val loggers: PartialFunction[String, Logger[IO]] = {
case "some.external.package.SpecificClass" =>
Expand Down
8 changes: 4 additions & 4 deletions slf4j/src/main/scala/io/odin/slf4j/OdinLoggerAdapter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@ package io.odin.slf4j
import java.util.concurrent.TimeUnit

import cats.Eval
import cats.effect.{ConcurrentEffect, Timer}
import cats.effect.{Clock, Effect}
import cats.syntax.all._
import io.odin.meta.Position
import io.odin.{Level, LoggerMessage, Logger => OdinLogger}
import org.slf4j.Logger
import org.slf4j.helpers.{FormattingTuple, MarkerIgnoringBase, MessageFormatter}

case class OdinLoggerAdapter[F[_]](loggerName: String, underlying: OdinLogger[F])(
implicit F: ConcurrentEffect[F],
timer: Timer[F]
implicit F: Effect[F],
clock: Clock[F]
) extends MarkerIgnoringBase
with Logger {

private def run(level: Level, msg: String, t: Option[Throwable] = None): Unit =
F.toIO(F.whenA(level >= underlying.minLevel)(for {
timestamp <- timer.clock.realTime(TimeUnit.MILLISECONDS)
timestamp <- clock.realTime(TimeUnit.MILLISECONDS)
_ <- underlying.log(
LoggerMessage(
level = level,
Expand Down
6 changes: 3 additions & 3 deletions slf4j/src/main/scala/io/odin/slf4j/OdinLoggerBinder.scala
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package io.odin.slf4j

import cats.effect.{ConcurrentEffect, Timer}
import cats.effect.{Clock, Effect}
import io.odin.Logger
import org.slf4j.ILoggerFactory
import org.slf4j.spi.LoggerFactoryBinder

abstract class OdinLoggerBinder[F[_]] extends LoggerFactoryBinder {

implicit def timer: Timer[F]
implicit def F: ConcurrentEffect[F]
implicit def clock: Clock[F]
implicit def F: Effect[F]

def loggers: PartialFunction[String, Logger[F]]

Expand Down
4 changes: 2 additions & 2 deletions slf4j/src/main/scala/io/odin/slf4j/OdinLoggerFactory.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package io.odin.slf4j

import cats.effect.{ConcurrentEffect, Timer}
import cats.effect.{Clock, Effect}
import io.odin.{Logger => OdinLogger}
import org.slf4j.{ILoggerFactory, Logger}

class OdinLoggerFactory[F[_]: ConcurrentEffect: Timer](loggers: PartialFunction[String, OdinLogger[F]])
class OdinLoggerFactory[F[_]: Effect: Clock](loggers: PartialFunction[String, OdinLogger[F]])
extends ILoggerFactory {
def getLogger(name: String): Logger = {
new OdinLoggerAdapter[F](name, loggers.applyOrElse(name, (_: String) => OdinLogger.noop))
Expand Down
5 changes: 3 additions & 2 deletions slf4j/src/test/scala/org/slf4j/impl/StaticLoggerBinder.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.slf4j.impl

import cats.effect.{ConcurrentEffect, ContextShift, IO, Timer}
import cats.effect.{Clock, ContextShift, Effect, IO, Timer}
import io.odin._
import io.odin.slf4j.{BufferingLogger, OdinLoggerBinder}

Expand All @@ -10,8 +10,9 @@ class StaticLoggerBinder extends OdinLoggerBinder[IO] {

val ec: ExecutionContext = scala.concurrent.ExecutionContext.global
implicit val timer: Timer[IO] = IO.timer(ec)
implicit val clock: Clock[IO] = timer.clock
implicit val cs: ContextShift[IO] = IO.contextShift(ec)
implicit val F: ConcurrentEffect[IO] = IO.ioConcurrentEffect
implicit val F: Effect[IO] = IO.ioEffect

val loggers: PartialFunction[String, Logger[IO]] = {
case Level.Trace.toString => new BufferingLogger[IO](Level.Trace)
Expand Down

0 comments on commit a91e22c

Please sign in to comment.