-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #73 from TinkoffCreditSystems/zio-logging
add logging support for ZIO
- Loading branch information
Showing
7 changed files
with
133 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
29
zio/logging/src/main/scala/tofu/logging/impl/UIOZLogging.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
60 changes: 60 additions & 0 deletions
60
zio/logging/src/main/scala/tofu/logging/impl/URIOZLoggingImpl.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |