Skip to content

Commit

Permalink
improve Log.console
Browse files Browse the repository at this point in the history
  • Loading branch information
Denys Fakhritdinov committed May 22, 2024
1 parent 66b7920 commit dc57801
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
28 changes: 20 additions & 8 deletions core/src/main/scala/com/evolutiongaming/catshelper/Log.scala
Original file line number Diff line number Diff line change
Expand Up @@ -220,23 +220,35 @@ object Log {

def empty[F[_]: Applicative]: Log[F] = const(Applicative[F].unit)

def console[F[_]: Monad: Console]: Log[F] = new Log[F] {
@deprecated("use `console(<name>)` instead", "3.10.5")
def console[F[_]: Monad: Console]: Log[F] = console("")

def console[F[_]: Monad: Console](name: String): Log[F] = new Log[F] {

val C = Console[F]

override def trace(msg: => String, mdc: Mdc): F[Unit] = C.println(s"TRACE $mdc: $msg")
def log(level: String, msg: => String, mdc: Log.Mdc): String = {
val mdcStr = mdc.context match {
case None => ""
case Some(mdc) => mdc.toSortedMap.map { case (k, v) => s"$k=$v" }.mkString(" ", ", ", "")
}
s"$level\t$name$mdcStr: $msg"
}


override def trace(msg: => String, mdc: Mdc): F[Unit] = C.println(log("TRACE", msg, mdc))

override def debug(msg: => String, mdc: Mdc): F[Unit] = C.println(s"DEBUG $mdc: $msg")
override def debug(msg: => String, mdc: Mdc): F[Unit] = C.println(log("DEBUG", msg, mdc))

override def info(msg: => String, mdc: Mdc): F[Unit] = C.println(s"INFO $mdc: $msg")
override def info(msg: => String, mdc: Mdc): F[Unit] = C.println(log("INFO", msg, mdc))

override def warn(msg: => String, mdc: Mdc): F[Unit] = C.println(s"WARN $mdc: $msg")
override def warn(msg: => String, mdc: Mdc): F[Unit] = C.println(log("WARN", msg, mdc))

override def warn(msg: => String, cause: Throwable, mdc: Mdc): F[Unit] = C.println(s"WARN $mdc: $msg") >> C.printStackTrace(cause)
override def warn(msg: => String, cause: Throwable, mdc: Mdc): F[Unit] = C.println(log("WARN", msg, mdc)) >> C.printStackTrace(cause)

override def error(msg: => String, mdc: Mdc): F[Unit] = C.errorln(s"ERROR $mdc: $msg")
override def error(msg: => String, mdc: Mdc): F[Unit] = C.errorln(log("ERRROR", msg, mdc))

override def error(msg: => String, cause: Throwable, mdc: Mdc): F[Unit] = C.errorln(s"ERROR $mdc: $msg") >> C.printStackTrace(cause)
override def error(msg: => String, cause: Throwable, mdc: Mdc): F[Unit] = C.errorln(log("ERROR", msg, mdc)) >> C.printStackTrace(cause)

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ object LogOf {
def empty[F[_] : Applicative]: LogOf[F] = const(Log.empty[F].pure[F])


def console[F[_]: Monad: Console]: LogOf[F] = const(Log.console[F].pure[F])
def console[F[_]: Monad: Console]: LogOf[F] = new LogOf[F] {

def apply(source: String) = Log.console[F](source).pure[F]

def apply(source: Class[?]) = apply(source.getName.stripSuffix("$"))
}

def const[F[_]](log: F[Log[F]]): LogOf[F] = new LogOf[F] {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ class LogSpec extends AnyFunSuite with Matchers {
}

test("Log.console") {
val log = Log.console[IO]
val log = Log.console[IO]("my-test-name")
val mdc = Log.Mdc.Lazy("answer" -> "42")
val io = for {
_ <- log.trace("trace msg", mdc)
Expand Down

0 comments on commit dc57801

Please sign in to comment.