Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 21 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ 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._
import cats.syntax.all.*

object MyThing {
// Impure But What 90% of Folks I know do with log4s
Expand All @@ -50,24 +50,24 @@ object MyThing {
// 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(())
}
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")}
.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 {
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")}
.onError { case e => Logger[F].error(e)("Something Went Wrong in passForEasierUse") }
_ <- Logger[F].info("Logging at end of passForEasierUse")
} yield something
}
Expand Down Expand Up @@ -97,12 +97,12 @@ 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._
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] =
def log[F[_]: Sync : Logger] =
for {
result1 <- successComputation[F]
_ <- info"First result is $result1"
Expand All @@ -128,10 +128,12 @@ If you are unsure how to create a new `LoggerFactory[F]` instance, then you can
or `log4cats-noop` modules for concrete implementations.

The quickest fix might be to import needed implicits:

```scala
// assumes dependency on log4cats-slf4j module
import org.typelevel.log4cats._
import org.typelevel.log4cats.slf4j._

import org.typelevel.log4cats.*
import org.typelevel.log4cats.slf4j.*

val logger: SelfAwareStructuredLogger[IO] = LoggerFactory[IO].getLogger

Expand All @@ -141,11 +143,12 @@ def anyFSyncLogger[F[_]: Sync]: SelfAwareStructuredLogger[F] = Slf4jFactory[F].g

Alternatively, a mutually exclusive solution is to explicitly create your
`LoggerFactory[F]` instance and pass them around implicitly:

```scala
import cats.effect.IO
import cats.Monad
import cats.syntax.all._
import org.typelevel.log4cats._
import cats.syntax.all.*
import org.typelevel.log4cats.*
import org.typelevel.log4cats.slf4j.Slf4jFactory

// create our LoggerFactory
Expand All @@ -156,14 +159,16 @@ 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] {
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")
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ private[log4cats] object SharedLoggerNameMacro {

/** Get a logger by reflecting the enclosing class name. */
private[log4cats] def getLoggerNameImpl(c: blackbox.Context) = {
import c.universe._
import c.universe.*

@tailrec def findEnclosingClass(sym: c.universe.Symbol): c.universe.Symbol = {
sym match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package org.typelevel.log4cats

import cats._
import cats.*
trait ErrorLogger[F[_]] {
def error(t: Throwable)(message: => String): F[Unit]
def warn(t: Throwable)(message: => String): F[Unit]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package org.typelevel.log4cats

import cats._
import cats.*
import cats.data.{EitherT, Kleisli, OptionT}

trait Logger[F[_]] extends MessageLogger[F] with ErrorLogger[F] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package org.typelevel.log4cats
import cats.Functor
import cats.Show.Shown
import cats.data.Kleisli
import cats.syntax.functor._
import cats.syntax.functor.*
import cats.~>
import cats.data.OptionT
import cats.data.EitherT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ package org.typelevel.log4cats
trait LoggerFactoryGen[F[_]] {
type LoggerType <: Logger[F]
def getLogger(implicit name: LoggerName): LoggerType = getLoggerFromName(name.value)
def getLoggerFromClass(clazz: Class[_]): LoggerType = getLoggerFromName(clazz.getName)
def getLoggerFromClass(clazz: Class[?]): LoggerType = getLoggerFromName(clazz.getName)
def create(implicit name: LoggerName): F[LoggerType] = fromName(name.value)
def fromClass(clazz: Class[_]): F[LoggerType] = fromName(clazz.getName)
def fromClass(clazz: Class[?]): F[LoggerType] = fromName(clazz.getName)
def getLoggerFromName(name: String): LoggerType
def fromName(name: String): F[LoggerType]
}
Expand All @@ -31,12 +31,12 @@ private[log4cats] trait LoggerFactoryGenCompanion {
lf.getLogger
def getLoggerFromName[F[_]](name: String)(implicit lf: LoggerFactoryGen[F]): lf.LoggerType =
lf.getLoggerFromName(name)
def getLoggerFromClass[F[_]](clazz: Class[_])(implicit lf: LoggerFactoryGen[F]): lf.LoggerType =
def getLoggerFromClass[F[_]](clazz: Class[?])(implicit lf: LoggerFactoryGen[F]): lf.LoggerType =
lf.getLoggerFromClass(clazz)
def create[F[_]](implicit lf: LoggerFactoryGen[F], name: LoggerName): F[lf.LoggerType] =
lf.create
def fromName[F[_]](name: String)(implicit lf: LoggerFactoryGen[F]): F[lf.LoggerType] =
lf.fromName(name)
def fromClass[F[_]](clazz: Class[_])(implicit lf: LoggerFactoryGen[F]): F[lf.LoggerType] =
def fromClass[F[_]](clazz: Class[?])(implicit lf: LoggerFactoryGen[F]): F[lf.LoggerType] =
lf.fromClass(clazz)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package org.typelevel.log4cats

import cats._
import cats.*

trait MessageLogger[F[_]] {
def error(message: => String): F[Unit]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

package org.typelevel.log4cats

import cats._
import cats.*
import cats.effect.std.UUIDGen
import cats.syntax.all._
import cats.syntax.all.*

import java.io.{PrintWriter, StringWriter}
import java.util.UUID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package org.typelevel.log4cats

import cats._
import cats.*

trait SelfAwareLogger[F[_]] extends Logger[F] {
def isTraceEnabled: F[Boolean]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package org.typelevel.log4cats

import cats._
import cats.*
import cats.Show.Shown

trait SelfAwareStructuredLogger[F[_]] extends SelfAwareLogger[F] with StructuredLogger[F] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package org.typelevel.log4cats

import cats._
import cats.*
import cats.Show.Shown

trait StructuredLogger[F[_]] extends Logger[F] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package org.typelevel.log4cats.extras
import cats.data.Chain
import cats.effect.kernel.Resource.ExitCase
import cats.effect.kernel.{Concurrent, Ref, Resource}
import cats.syntax.all._
import cats.syntax.all.*
import cats.~>
import org.typelevel.log4cats.Logger

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package org.typelevel.log4cats.extras
import cats.Show.Shown
import cats.data.Chain
import cats.effect.kernel.{Concurrent, Resource}
import cats.syntax.all._
import cats.syntax.all.*
import cats.{~>, Functor}
import org.typelevel.log4cats.{LoggerFactory, SelfAwareStructuredLogger}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import cats.Show.Shown
import cats.data.Chain
import cats.effect.kernel.Resource.ExitCase
import cats.effect.kernel.{Concurrent, Ref, Resource}
import cats.syntax.all._
import cats.syntax.all.*
import cats.~>
import org.typelevel.log4cats.StructuredLogger

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package org.typelevel.log4cats.extras

import cats._
import cats.*

sealed trait LogLevel
object LogLevel {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

package org.typelevel.log4cats.extras

import cats._
import cats.syntax.all._
import cats.*
import cats.syntax.all.*
import org.typelevel.log4cats.Logger

final case class LogMessage(level: LogLevel, t: Option[Throwable], message: String)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package org.typelevel.log4cats.extras

import cats.Show
import cats.syntax.all._
import cats.syntax.all.*
import org.typelevel.log4cats.StructuredLogger

final case class StructuredLogMessage(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@

package org.typelevel.log4cats.extras

import cats._
import cats.data._
import cats.syntax.all._
import org.typelevel.log4cats._
import cats.*
import cats.data.*
import cats.syntax.all.*
import org.typelevel.log4cats.*

/**
* A `SelfAwareLogger` implemented using `cats.data.Writer`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package org.typelevel.log4cats.extras

import cats.data.Writer
import cats.syntax.all._
import cats.syntax.all.*
import cats.{~>, Alternative, Applicative, Foldable, Id}
import org.typelevel.log4cats.{SelfAwareStructuredLogger, StructuredLogger}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@

package org.typelevel.log4cats.extras

import cats._
import cats.data._
import cats.syntax.all._
import org.typelevel.log4cats._
import cats.*
import cats.data.*
import cats.syntax.all.*
import org.typelevel.log4cats.*

/**
* A `SelfAwareLogger` implemented using `cats.data.WriterT`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package org.typelevel.log4cats.extras

import cats.data.WriterT
import cats.kernel.Monoid
import cats.syntax.all._
import cats.syntax.all.*
import cats.{~>, Alternative, Applicative, Foldable, Monad}
import org.typelevel.log4cats.{SelfAwareStructuredLogger, StructuredLogger}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ package org.typelevel.log4cats
package object syntax {
implicit final class LoggerInterpolator(private val sc: StringContext) extends AnyVal {
def error[F[_]](message: Any*)(implicit logger: Logger[F]): F[Unit] =
logger.error(sc.s(message: _*))
logger.error(sc.s(message*))

def warn[F[_]](message: Any*)(implicit logger: Logger[F]): F[Unit] =
logger.warn(sc.s(message: _*))
logger.warn(sc.s(message*))

def info[F[_]](message: Any*)(implicit logger: Logger[F]): F[Unit] =
logger.info(sc.s(message: _*))
logger.info(sc.s(message*))

def debug[F[_]](message: Any*)(implicit logger: Logger[F]): F[Unit] =
logger.debug(sc.s(message: _*))
logger.debug(sc.s(message*))

def trace[F[_]](message: Any*)(implicit logger: Logger[F]): F[Unit] =
logger.trace(sc.s(message: _*))
logger.trace(sc.s(message*))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package org.typelevel.log4cats.extras.syntax

import cats._
import cats.*
import cats.data.EitherT
import cats.data.Kleisli
import cats.data.OptionT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

package org.typelevel.log4cats.extras.syntax

import cats._
import cats.*
import cats.data.{EitherT, Kleisli, OptionT}
import org.typelevel.log4cats._
import org.typelevel.log4cats.*

object LoggerSyntaxCompilation {

Expand Down
Loading