Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/christopherdavenport/log4cats-mtl
Log4cats MTL Integration Layer
https://github.com/christopherdavenport/log4cats-mtl
Last synced: about 1 month ago
JSON representation
Log4cats MTL Integration Layer
- Host: GitHub
- URL: https://github.com/christopherdavenport/log4cats-mtl
- Owner: ChristopherDavenport
- License: mit
- Created: 2019-09-30T13:45:34.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2019-10-04T20:38:27.000Z (about 5 years ago)
- Last Synced: 2023-07-03T20:05:59.864Z (over 1 year ago)
- Language: Scala
- Homepage: https://christopherdavenport.github.io/log4cats-mtl
- Size: 319 KB
- Stars: 3
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# log4cats-mtl - Log4cats MTL Integration Layer [![Build Status](https://travis-ci.com/ChristopherDavenport/log4cats-mtl.svg?branch=master)](https://travis-ci.com/ChristopherDavenport/log4cats-mtl) [![Maven Central](https://maven-badges.herokuapp.com/maven-central/io.chrisdavenport/log4cats-mtl_2.12/badge.svg)](https://maven-badges.herokuapp.com/maven-central/io.chrisdavenport/log4cats-mtl_2.12) ![Code of Consuct](https://img.shields.io/badge/Code%20of%20Conduct-Scala-blue.svg)
## [Head on over to the microsite](https://ChristopherDavenport.github.io/log4cats-mtl)
## 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:```scala
libraryDependencies ++= Seq(
"io.chrisdavenport" %% "log4cats-mtl" % ""
)
```## Examples
```scala
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
}
```