Project Name | Stars | Downloads | Repos Using This | Packages Using This | Most Recent Commit | Total Releases | Latest Release | Open Issues | License | Language |
---|---|---|---|---|---|---|---|---|---|---|
Logcat | 510 | 2 years ago | 1 | September 21, 2021 | 3 | apache-2.0 | Kotlin | |||
I CAN HAZ LOGZ? | ||||||||||
Log4cats | 377 | 67 | 5 days ago | 20 | May 12, 2020 | 16 | apache-2.0 | Scala | ||
Logging Tools For Interaction with cats-effect | ||||||||||
Treelog | 212 | 10 days ago | 13 | January 10, 2022 | 3 | mit | Scala | |||
Allows logging in a tree structure so that comprehensive logging does not become incomprehensible | ||||||||||
Nyan Progress Webpack Plugin | 160 | 187 | 69 | 4 years ago | 10 | March 16, 2017 | 3 | mit | JavaScript | |
Meow | ||||||||||
Go Logsink | 35 | a year ago | 19 | May 30, 2021 | 2 | apache-2.0 | Go | |||
the multi machine cat over protocol buffers and websocket | ||||||||||
Cat | 31 | 21 | 1 | 6 years ago | 6 | September 14, 2016 | apache-2.0 | Java | ||
This is a simple logging library for Android. | ||||||||||
Log Effect | 20 | 6 | 4 days ago | 54 | March 15, 2022 | 1 | mit | Scala | ||
A simple macro-less logging typeclass with some common backends | ||||||||||
Logdna Android Client | 16 | 4 years ago | apache-2.0 | Kotlin | ||||||
Android client for LogDNA | ||||||||||
Logshell | 8 | 4 years ago | mit | Shell | ||||||
Logs a shell session to a file. | ||||||||||
Logger4s | 7 | 5 years ago | 1 | Scala | ||||||
Purely functional Logger for Scala |
log4cats attempts 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.
To use log4cats in an existing SBT project with Scala 2.12 or a later version, add the following dependency to your
build.sbt
:
libraryDependencies ++= Seq(
"org.typelevel" %% "log4cats-core" % "<version>", // Only if you want to Support Any Backend
"org.typelevel" %% "log4cats-slf4j" % "<version>", // Direct Slf4j Support - Recommended
)
Well, to answer that, let's take a look at how you might combine cats-effect with vanilla logging...
object MyVanillaLoggingThing {
val logger: Logger = Logger(LoggerFactory.getLogger(getClass.getName))
def doSomething(): IO[Unit] =
IO(logger.info("Doing something!")) *> IO.println("Hello, World!")
}
But what if you don't want to wrap your logger in an IO
like this?
Good news, you don't have to! Enter log4cats! Read on!
import org.typelevel.log4cats.Logger
import org.typelevel.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.getLogger[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
}
If you compare the vanilla logger + cats-effect example with the log4cats examples above, you might be asking yourself "why do I have to do any of this?" or "why can't I just add a log statement like normal?"
Well there are several reasons. Logging is often times an overlooked side-effect,
which under the hood can range from writing to a mutable queue, writing to disk,
outputting to the console, or sometimes even doing network I/O!
To correctly deal with these kinds of side-effects we have to ensure they are properly wrapped in IO
,
see the cats-effect docs for more details.
Basically, we are using cats-effect. We like things like "referential transparency"
and "programs-as-values". Wrapping our log statement in an IO
helps with that.
It's possible to use interpolated syntax for logging.
Currently, supported ops are: trace
, debug
, info
, warn
, error
.
You can use it for your custom Logger
as well as for Slf4j Logger
.
import cats.Applicative
import cats.effect.Sync
import org.typelevel.log4cats.Logger
import org.typelevel.log4cats.syntax._
def successComputation[F[_]: Applicative]: F[Int] = Applicative[F].pure(1)
def errorComputation[F[_]: Sync]: F[Unit] = Sync[F].raiseError[Unit](new Throwable("Sorry!"))
def log[F[_]: Sync: Logger] =
for {
result1 <- successComputation[F]
_ <- info"First result is $result1"
_ <- errorComputation[F].onError(_ => error"We got an error!")
} yield ()
You can work with logging using capabilities. It's implemented via the LoggerFactory
trait.
You instantiate it once (dependent on the specific logging backend you use)
and pass this around in your application.
This brings several advantages:
F[_]: Sync
constraint everywhere
that can do almost anything when you only need logging.If you are unsure how to create a new LoggerFactory[F]
instance, then you can look at the log4cats-slf4j
,
or log4cats-noop
modules for concrete implementations.
The quickest fix might be to import needed implicits:
// assumes dependency on log4cats-slf4j module
import org.typelevel.log4cats._
import org.typelevel.log4cats.slf4j._
val logger: SelfAwareStructuredLogger[IO] = LoggerFactory[IO].getLogger
// or
def anyFSyncLogger[F[_]: Sync]: SelfAwareStructuredLogger[F] = Slf4jFactory[F].getLogger
Alternatively, a mutually exclusive solution is to explicitly create your
LoggerFactory[F]
instance and pass them around implicitly:
import cats.effect.IO
import cats.Monad
import cats.syntax.all._
import org.typelevel.log4cats._
import org.typelevel.log4cats.slf4j.Slf4jFactory
// create our LoggerFactory
implicit val logging: LoggerFactory[IO] = Slf4jFactory[IO]
// we summon LoggerFactory instance, and create logger
val logger: SelfAwareStructuredLogger[IO] = LoggerFactory[IO].getLogger
logger.info("logging in IO!"): IO[Unit]
// basic example of a service using LoggerFactory
class LoggerUsingService[F[_]: LoggerFactory: Monad] {
val logger = LoggerFactory[F].getLogger
def use(args: String): F[Unit] =
for {
_ <- logger.info("yay! effect polymorphic code")
_ <- logger.debug(s"and $args")
} yield ()
}
new LoggerUsingService[IO].use("foo")
See here for details
log4cats is not directly susceptible to CVS-2021-44228. The log4cats-slf4j implementation delegates all logging operations to slf4j. if you use log4cats-slf4j, your configured slf4j provider may put you at risk. See slf4j's comments on CVE-2021-44228 for more.