christopherdavenport / log4cats-mtl   0.1.0

MIT License Website GitHub

Log4cats MTL Integration Layer

Scala versions: 2.13 2.12
Scala.js versions: 0.6

log4cats-mtl - Log4cats MTL Integration Layer Build Status Maven Central Code of Consuct

Quick Start

To use log4cats-mtl in an existing SBT project with Scala 2.12 or a later version, add the following dependencies to your build.sbt depending on your needs:

libraryDependencies ++= Seq(
  "io.chrisdavenport" %% "log4cats-mtl" % "<version>"
)

Examples

import io.chrisdavenport.log4cats.extras.LogMessage
import io.chrisdavenport.log4cats.slf4j.Slf4jLogger
import io.chrisdavenport.log4cats.mtl._
import cats._
import cats.data._
import cats.effect.Sync
import cats.implicits._
import cats.mtl._
import cats.mtl.implicits._

final case class TraceId(value: String)

implicit val traceIdCtxEncoder: CtxEncoder[TraceId] = (traceId: TraceId) => Map("traceId" -> traceId.value)

def doApplicativeLocalThings[F[_]: Sync: ApplicativeLocal[?[_], TraceId]]: F[Unit] = 
  (for {
    logger <- ApplicativeAskLogger.create[F, TraceId](Slf4jLogger.create[F])
    _ <- logger.info("Logging at start of safelyDoThings").scope(TraceId("inner-id"))
    something <- Sync[F].delay(println("I could do anything"))
      .onError{case e => logger.error(e)("Something Went Wrong in safelyDoThings")}
    _ <- logger.info("Logging at end of safelyDoThings")
  } yield something).scope(TraceId("outter-id"))
  
def doFunctorTellThings[F[_]: Sync: FunctorTell[?[_], Chain[LogMessage]]]: F[Unit] = {
  val logger = FunctorTellLogger[F, Chain]()

  for {
    _ <- logger.info("Logging at start of safelyDoThings")
    something <- Sync[F].delay(println("I could do anything"))
      .onError{case e => logger.error(e)("Something Went Wrong in safelyDoThings")}
    _ <- logger.info("Logging at end of safelyDoThings")
  } yield something
}