Skip to content
Logging Tools For Interaction with cats-effect
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
cats
core/shared/src/main/scala/io/chrisdavenport/log4cats
docs/src/main/tut
project Update sbt-updates to 0.4.0 Feb 11, 2019
scalaz/log4s/src/main/scala/io/chrisdavenport/log4scalaz/log4s
.gitignore Modifications to underlying Logging construction Jan 17, 2019
.scalafmt.conf Initial Commit Apr 24, 2018
.travis.yml Updated For StructuredLogger Modifications Jan 17, 2019
CHANGELOG.md
CODE_OF_CONDUCT.md
LICENSE Switch to Apache Licensing May 22, 2018
README.md
build.sbt Update specs2-core to 4.5.1 Mar 11, 2019
version.sbt

README.md

log4cats / log4scalaz Build Status Maven Central

Project Goals

log4cats and log4scalaz attempt to make referentially transparent logging a reality. These F algebras allow you to write code for logging knowing you won't be doing side-effects as it offers no way to do so. We provide our own slf4j layer, or you can use any of the supported backends, or create your own.

Quick Start

To use log4cats in an existing SBT project with Scala 2.11 or a later version, add the following dependency to your build.sbt:

libraryDependencies ++= Seq(
  "io.chrisdavenport" %% "log4cats-core"    % "<version>",  // Only if you want to Support Any Backend
  "io.chrisdavenport" %% "log4cats-extras"  % "<version>",  // Cats Extras - mapK, Writer, etc.
  "io.chrisdavenport" %% "log4cats-slf4j"   % "<version>",  // Direct Slf4j Support - Recommended
  "io.chrisdavenport" %% "log4cats-log4s"   % "<version>",  // For Log4s Support
  "io.chrisdavenport" %% "log4cats-scribe"  % "<version>",   // For Scribe Support
  "io.chrisdavenport" %% "log4scalaz-log4s" % "<version>"   // Scalaz Log4s Support
)

Examples

import io.chrisdavenport.log4cats.Logger
import io.chrisdavenport.log4cats.slf4j.Slf4jLogger
import cats.effect.Sync
import cats.implicits._

object MyThing {
  // Impure But What 90% of Folks I know do with log4s
  implicit def unsafeLogger[F[_]: Sync] = Slf4jLogger.unsafeCreate[F]

  // Arbitrary Local Function Declaration
  def doSomething[F[_]: Sync]: F[Unit] =
    Logger[F].info("Logging Start Something") *>
    Sync[F].delay(println("I could be doing anything"))
      .attempt.flatMap{
        case Left(e) => Logger[F].error(e)("Something Went Wrong")
        case Right(_) => Sync[F].pure(())
      }

  def safelyDoThings[F[_]: Sync]: F[Unit] = for {
    logger <- Slf4jLogger.create[F]
    _ <- 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

  def passForEasierUse[F[_]: Sync: Logger] = for {
    _ <- Logger[F].info("Logging at start of passForEasierUse")
    something <- Sync[F].delay(println("I could do anything"))
      .onError{case e => Logger[F].error(e)("Something Went Wrong in passForEasierUse")}
    _ <- Logger[F].info("Logging at end of passForEasierUse")
  } yield something
}
You can’t perform that action at this time.