Skip to content

Commit

Permalink
Merge pull request #73 from TinkoffCreditSystems/zio-logging
Browse files Browse the repository at this point in the history
add logging support for ZIO
  • Loading branch information
Odomontois authored Dec 18, 2019
2 parents ec00b41 + 7c6416e commit 4e5b750
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 14 deletions.
10 changes: 8 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,20 @@ lazy val zioCore =
.settings(defaultSettings, libraryDependencies ++= List(zio, zioCats))
.dependsOn(core, concurrent)

lazy val zioLogging =
project
.in(file("zio/logging"))
.settings(defaultSettings, libraryDependencies ++= List(zio, slf4j))
.dependsOn(loggingStr)

lazy val zioInterop = project
.in(file("zio"))
.settings(
publishName := "zio-interop",
defaultSettings
)
.dependsOn(zioCore)
.aggregate(zioCore)
.dependsOn(zioCore, zioLogging)
.aggregate(zioCore, zioLogging)

lazy val coreModules = Seq(core, memo, env, concurrent, opticsCore, data)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ class ContextLoggingImpl[F[_]: Applicative, C: Loggable, Service: ClassTag](cont
context.ask(ctx => logger.error(ContextMarker(ctx).addMarker(marker), message, values: _*)).whenA(errorEnabled)

override def traceCause(message: String, cause: Throwable, values: LoggedValue*): F[Unit] =
context.ask(ctx => logger.trace(ContextMarker(ctx), message, values :+ cause: _*)).whenA(errorEnabled)
context.ask(ctx => logger.trace(ContextMarker(ctx), message, values :+ cause: _*)).whenA(traceEnabled)
override def debugCause(message: String, cause: Throwable, values: LoggedValue*): F[Unit] =
context.ask(ctx => logger.debug(ContextMarker(ctx), message, values :+ cause: _*)).whenA(errorEnabled)
context.ask(ctx => logger.debug(ContextMarker(ctx), message, values :+ cause: _*)).whenA(debugEnabled)
override def infoCause(message: String, cause: Throwable, values: LoggedValue*): F[Unit] =
context.ask(ctx => logger.info(ContextMarker(ctx), message, values :+ cause: _*)).whenA(errorEnabled)
context.ask(ctx => logger.info(ContextMarker(ctx), message, values :+ cause: _*)).whenA(infoEnabled)
override def warnCause(message: String, cause: Throwable, values: LoggedValue*): F[Unit] =
context.ask(ctx => logger.error(ContextMarker(ctx), message, values :+ cause: _*)).whenA(errorEnabled)
context.ask(ctx => logger.error(ContextMarker(ctx), message, values :+ cause: _*)).whenA(warnEnabled)
override def errorCause(message: String, cause: Throwable, values: LoggedValue*): F[Unit] =
context.ask(ctx => logger.error(ContextMarker(ctx), message, values :+ cause: _*)).whenA(errorEnabled)
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ class ContextSyncLoggingImpl[F[_], C: Loggable](context: F HasContext C, logger:
.whenA(errorEnabled)

override def traceCause(message: String, cause: Throwable, values: LoggedValue*): F[Unit] =
context.askF(ctx => F.delay(logger.trace(ContextMarker(ctx), message, values :+ cause: _*))).whenA(errorEnabled)
context.askF(ctx => F.delay(logger.trace(ContextMarker(ctx), message, values :+ cause: _*))).whenA(traceEnabled)
override def debugCause(message: String, cause: Throwable, values: LoggedValue*): F[Unit] =
context.askF(ctx => F.delay(logger.debug(ContextMarker(ctx), message, values :+ cause: _*))).whenA(errorEnabled)
context.askF(ctx => F.delay(logger.debug(ContextMarker(ctx), message, values :+ cause: _*))).whenA(debugEnabled)
override def infoCause(message: String, cause: Throwable, values: LoggedValue*): F[Unit] =
context.askF(ctx => F.delay(logger.info(ContextMarker(ctx), message, values :+ cause: _*))).whenA(errorEnabled)
context.askF(ctx => F.delay(logger.info(ContextMarker(ctx), message, values :+ cause: _*))).whenA(infoEnabled)
override def warnCause(message: String, cause: Throwable, values: LoggedValue*): F[Unit] =
context.askF(ctx => F.delay(logger.warn(ContextMarker(ctx), message, values :+ cause: _*))).whenA(errorEnabled)
context.askF(ctx => F.delay(logger.warn(ContextMarker(ctx), message, values :+ cause: _*))).whenA(warnEnabled)
override def errorCause(message: String, cause: Throwable, values: LoggedValue*): F[Unit] =
context.askF(ctx => F.delay(logger.error(ContextMarker(ctx), message, values :+ cause: _*))).whenA(errorEnabled)
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ class SyncLogging[F[_]](logger: Logger)(implicit F: Sync[F]) extends LoggingImpl
override def errorCause(message: String, cause: Throwable, values: LoggedValue*): F[Unit] =
F.delay(logger.error(message, values :+ cause: _*)).whenA(errorEnabled)
override def warnCause(message: String, cause: Throwable, values: LoggedValue*): F[Unit] =
F.delay(logger.warn(message, values :+ cause: _*)).whenA(errorEnabled)
F.delay(logger.warn(message, values :+ cause: _*)).whenA(warnEnabled)
override def infoCause(message: String, cause: Throwable, values: LoggedValue*): F[Unit] =
F.delay(logger.info(message, values :+ cause: _*)).whenA(errorEnabled)
F.delay(logger.info(message, values :+ cause: _*)).whenA(infoEnabled)
override def debugCause(message: String, cause: Throwable, values: LoggedValue*): F[Unit] =
F.delay(logger.debug(message, values :+ cause: _*)).whenA(errorEnabled)
F.delay(logger.debug(message, values :+ cause: _*)).whenA(debugEnabled)
override def traceCause(message: String, cause: Throwable, values: LoggedValue*): F[Unit] =
F.delay(logger.trace(message, values :+ cause: _*)).whenA(errorEnabled)
F.delay(logger.trace(message, values :+ cause: _*)).whenA(traceEnabled)
}
24 changes: 24 additions & 0 deletions zio/logging/src/main/scala/tofu/logging/ZLogs.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package tofu.logging
import org.slf4j.LoggerFactory
import tofu.logging.Logging.loggerForService
import tofu.logging.impl.{UIOZLogging, URIOZLoggingImpl}
import zio.{UIO, URIO}

import scala.reflect.ClassTag

trait ZLogs[R] extends Logs[UIO, URIO[R, *]]

object ZLogs {
val urio: Logs[UIO, UIO] = new Logs[UIO, UIO] {
def forService[Svc: ClassTag]: UIO[Logging[UIO]] =
UIO.effectTotal(new UIOZLogging(loggerForService[Svc]))
def byName(name: String): UIO[Logging[UIO]] = UIO.effectTotal(new UIOZLogging(LoggerFactory.getLogger(name)))
}

def withContext[R: Loggable]: Logs[UIO, URIO[R, *]] = new Logs[UIO, URIO[R, *]] {
def forService[Svc: ClassTag]: UIO[Logging[URIO[R, *]]] =
UIO.effectTotal(new URIOZLoggingImpl(loggerForService[Svc]))
override def byName(name: String): UIO[Logging[URIO[R, *]]] =
UIO.effectTotal(new URIOZLoggingImpl[R](LoggerFactory.getLogger(name)))
}
}
29 changes: 29 additions & 0 deletions zio/logging/src/main/scala/tofu/logging/impl/UIOZLogging.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package tofu.logging.impl

import org.slf4j.Logger
import tofu.logging.LoggedValue
import zio.UIO

class UIOZLogging(logger: Logger) extends LoggingImpl[UIO](logger) {
override def trace(message: String, values: LoggedValue*) =
UIO.effectTotal(logger.trace(message, values: _*)).when(traceEnabled)
override def debug(message: String, values: LoggedValue*) =
UIO.effectTotal(logger.debug(message, values: _*)).when(debugEnabled)
override def info(message: String, values: LoggedValue*) =
UIO.effectTotal(logger.info(message, values: _*)).when(infoEnabled)
override def warn(message: String, values: LoggedValue*) =
UIO.effectTotal(logger.warn(message, values: _*)).when(warnEnabled)
override def error(message: String, values: LoggedValue*) =
UIO.effectTotal(logger.error(message, values: _*)).when(errorEnabled)

override def errorCause(message: String, cause: Throwable, values: LoggedValue*): UIO[Unit] =
UIO.effectTotal(logger.error(message, values :+ cause: _*)).when(errorEnabled)
override def warnCause(message: String, cause: Throwable, values: LoggedValue*): UIO[Unit] =
UIO.effectTotal(logger.warn(message, values :+ cause: _*)).when(warnEnabled)
override def infoCause(message: String, cause: Throwable, values: LoggedValue*): UIO[Unit] =
UIO.effectTotal(logger.info(message, values :+ cause: _*)).when(infoEnabled)
override def debugCause(message: String, cause: Throwable, values: LoggedValue*): UIO[Unit] =
UIO.effectTotal(logger.debug(message, values :+ cause: _*)).when(debugEnabled)
override def traceCause(message: String, cause: Throwable, values: LoggedValue*): UIO[Unit] =
UIO.effectTotal(logger.trace(message, values :+ cause: _*)).when(traceEnabled)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package tofu.logging.impl

import tofu.logging.{Loggable, LoggedValue}
import org.slf4j.{Logger, Marker}
import zio.{UIO, URIO}

class URIOZLoggingImpl[R: Loggable](logger: Logger) extends LoggingImpl[URIO[R, *]](logger) {
override def trace(message: String, values: LoggedValue*) =
URIO.accessM[R](ctx => UIO.effectTotal(logger.trace(ContextMarker(ctx), message, values: _*))).when(traceEnabled)
override def debug(message: String, values: LoggedValue*) =
URIO.accessM[R](ctx => UIO.effectTotal(logger.debug(ContextMarker(ctx), message, values: _*))).when(debugEnabled)
override def info(message: String, values: LoggedValue*) =
URIO.accessM[R](ctx => UIO.effectTotal(logger.info(ContextMarker(ctx), message, values: _*))).when(infoEnabled)
override def warn(message: String, values: LoggedValue*) =
URIO.accessM[R](ctx => UIO.effectTotal(logger.warn(ContextMarker(ctx), message, values: _*))).when(warnEnabled)
override def error(message: String, values: LoggedValue*) =
URIO.accessM[R](ctx => UIO.effectTotal(logger.error(ContextMarker(ctx), message, values: _*))).when(errorEnabled)

override def traceWithMarker(message: String, marker: Marker, values: LoggedValue*): URIO[R, Unit] =
URIO
.accessM[R](ctx => UIO.effectTotal(logger.trace(ContextMarker(ctx).addMarker(marker), message, values: _*)))
.when(traceEnabled)
override def debugWithMarker(message: String, marker: Marker, values: LoggedValue*): URIO[R, Unit] =
URIO
.accessM[R](ctx => UIO.effectTotal(logger.debug(ContextMarker(ctx).addMarker(marker), message, values: _*)))
.when(debugEnabled)
override def infoWithMarker(message: String, marker: Marker, values: LoggedValue*): URIO[R, Unit] =
URIO
.accessM[R](ctx => UIO.effectTotal(logger.info(ContextMarker(ctx).addMarker(marker), message, values: _*)))
.when(infoEnabled)
override def warnWithMarker(message: String, marker: Marker, values: LoggedValue*): URIO[R, Unit] =
URIO
.accessM[R](ctx => UIO.effectTotal(logger.warn(ContextMarker(ctx).addMarker(marker), message, values: _*)))
.when(warnEnabled)
override def errorWithMarker(message: String, marker: Marker, values: LoggedValue*): URIO[R, Unit] =
URIO
.accessM[R](ctx => UIO.effectTotal(logger.error(ContextMarker(ctx).addMarker(marker), message, values: _*)))
.when(errorEnabled)

override def traceCause(message: String, cause: Throwable, values: LoggedValue*): URIO[R, Unit] =
URIO
.accessM[R](ctx => UIO.effectTotal(logger.trace(ContextMarker(ctx), message, values :+ cause: _*)))
.when(traceEnabled)
override def debugCause(message: String, cause: Throwable, values: LoggedValue*): URIO[R, Unit] =
URIO
.accessM[R](ctx => UIO.effectTotal(logger.debug(ContextMarker(ctx), message, values :+ cause: _*)))
.when(debugEnabled)
override def infoCause(message: String, cause: Throwable, values: LoggedValue*): URIO[R, Unit] =
URIO
.accessM[R](ctx => UIO.effectTotal(logger.info(ContextMarker(ctx), message, values :+ cause: _*)))
.when(infoEnabled)
override def warnCause(message: String, cause: Throwable, values: LoggedValue*): URIO[R, Unit] =
URIO
.accessM[R](ctx => UIO.effectTotal(logger.warn(ContextMarker(ctx), message, values :+ cause: _*)))
.when(warnEnabled)
override def errorCause(message: String, cause: Throwable, values: LoggedValue*): URIO[R, Unit] =
URIO
.accessM[R](ctx => UIO.effectTotal(logger.error(ContextMarker(ctx), message, values :+ cause: _*)))
.when(errorEnabled)
}

0 comments on commit 4e5b750

Please sign in to comment.