Skip to content

Commit

Permalink
Merge 064680b into afd71ff
Browse files Browse the repository at this point in the history
  • Loading branch information
DeviLab committed Oct 6, 2023
2 parents afd71ff + 064680b commit b7ee222
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 91 deletions.
12 changes: 1 addition & 11 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,7 @@ lazy val logback = project
libraryDependencies ++= Seq(
Logback.classic,
scalatest % Test,
),
libraryDependencies ++= crossSettings(
scalaVersion.value,
if3 = Nil,
if2 = List(compilerPlugin("org.typelevel" % "kind-projector" % "0.13.2" cross CrossVersion.full))
),
scalacOptions ++= crossSettings(
scalaVersion.value,
if3 = Seq("-Ykind-projector:underscores", "-language:implicitConversions"),
if2 = List("-Xsource:3", "-P:kind-projector:underscore-placeholders")
),
)
)
.dependsOn(
core,
Expand Down
71 changes: 35 additions & 36 deletions core/src/main/scala/com/evolutiongaming/catshelper/Log.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@ trait Log[F[_]] {

@inline def error(msg: => String, cause: Throwable): F[Unit] = error(msg, cause, mdc = Log.Mdc.empty)

def trace(msg: => String, mdc: => Log.Mdc): F[Unit]
def trace(msg: => String, mdc: Log.Mdc): F[Unit]

def debug(msg: => String, mdc: => Log.Mdc): F[Unit]
def debug(msg: => String, mdc: Log.Mdc): F[Unit]

def info(msg: => String, mdc: => Log.Mdc): F[Unit]
def info(msg: => String, mdc: Log.Mdc): F[Unit]

def warn(msg: => String, mdc: => Log.Mdc): F[Unit]
def warn(msg: => String, mdc: Log.Mdc): F[Unit]

def warn(msg: => String, cause: Throwable, mdc: => Log.Mdc): F[Unit]
def warn(msg: => String, cause: Throwable, mdc: Log.Mdc): F[Unit]

def error(msg: => String, mdc: => Log.Mdc): F[Unit]
def error(msg: => String, mdc: Log.Mdc): F[Unit]

def error(msg: => String, cause: Throwable, mdc: => Log.Mdc): F[Unit]
def error(msg: => String, cause: Throwable, mdc: Log.Mdc): F[Unit]
}

object Log {
Expand Down Expand Up @@ -91,44 +91,43 @@ object Log {
}
}


def trace(msg: => String, mdc: => Log.Mdc) = {
def trace(msg: => String, mdc: Log.Mdc) = {
Sync[F].delay {
if (logger.isTraceEnabled) withMDC(mdc) { logger.trace(msg) }
}
}

def debug(msg: => String, mdc: => Log.Mdc) = {
def debug(msg: => String, mdc: Log.Mdc) = {
Sync[F].delay {
if (logger.isDebugEnabled) withMDC(mdc) { logger.debug(msg) }
}
}

def info(msg: => String, mdc: => Log.Mdc) = {
def info(msg: => String, mdc: Log.Mdc) = {
Sync[F].delay {
if (logger.isInfoEnabled) withMDC(mdc) { logger.info(msg) }
}
}

def warn(msg: => String, mdc: => Log.Mdc) = {
def warn(msg: => String, mdc: Log.Mdc) = {
Sync[F].delay {
if (logger.isWarnEnabled) withMDC(mdc) { logger.warn(msg) }
}
}

def warn(msg: => String, cause: Throwable, mdc: => Log.Mdc) = {
def warn(msg: => String, cause: Throwable, mdc: Log.Mdc) = {
Sync[F].delay {
if (logger.isWarnEnabled) withMDC(mdc) { logger.warn(msg, cause) }
}
}

def error(msg: => String, mdc: => Log.Mdc) = {
def error(msg: => String, mdc: Log.Mdc) = {
Sync[F].delay {
if (logger.isErrorEnabled) withMDC(mdc) { logger.error(msg) }
}
}

def error(msg: => String, cause: Throwable, mdc: => Log.Mdc) = {
def error(msg: => String, cause: Throwable, mdc: Log.Mdc) = {
Sync[F].delay {
if (logger.isErrorEnabled) withMDC(mdc) { logger.error(msg, cause) }
}
Expand All @@ -137,19 +136,19 @@ object Log {

def const[F[_]](unit: F[Unit]): Log[F] = new Log[F] {

def trace(msg: => String, mdc: => Log.Mdc) = unit
def trace(msg: => String, mdc: Log.Mdc) = unit

def debug(msg: => String, mdc: => Log.Mdc) = unit
def debug(msg: => String, mdc: Log.Mdc) = unit

def info(msg: => String, mdc: => Log.Mdc) = unit
def info(msg: => String, mdc: Log.Mdc) = unit

def warn(msg: => String, mdc: => Log.Mdc) = unit
def warn(msg: => String, mdc: Log.Mdc) = unit

def warn(msg: => String, cause: Throwable, mdc: => Log.Mdc) = unit
def warn(msg: => String, cause: Throwable, mdc: Log.Mdc) = unit

def error(msg: => String, mdc: => Log.Mdc) = unit
def error(msg: => String, mdc: Log.Mdc) = unit

def error(msg: => String, cause: Throwable, mdc: => Log.Mdc) = unit
def error(msg: => String, cause: Throwable, mdc: Log.Mdc) = unit
}

def empty[F[_]: Applicative]: Log[F] = const(Applicative[F].unit)
Expand All @@ -158,36 +157,36 @@ object Log {

def mapK[G[_]](f: F ~> G): Log[G] = new Log[G] {

def trace(msg: => String, mdc: => Log.Mdc) = f(self.trace(msg, mdc))
def trace(msg: => String, mdc: Log.Mdc) = f(self.trace(msg, mdc))

def debug(msg: => String, mdc: => Log.Mdc) = f(self.debug(msg, mdc))
def debug(msg: => String, mdc: Log.Mdc) = f(self.debug(msg, mdc))

def info(msg: => String, mdc: => Log.Mdc) = f(self.info(msg, mdc))
def info(msg: => String, mdc: Log.Mdc) = f(self.info(msg, mdc))

def warn(msg: => String, mdc: => Log.Mdc) = f(self.warn(msg, mdc))
def warn(msg: => String, mdc: Log.Mdc) = f(self.warn(msg, mdc))

def warn(msg: => String, cause: Throwable, mdc: => Log.Mdc) = f(self.warn(msg, cause, mdc))
def warn(msg: => String, cause: Throwable, mdc: Log.Mdc) = f(self.warn(msg, cause, mdc))

def error(msg: => String, mdc: => Log.Mdc) = f(self.error(msg, mdc))
def error(msg: => String, mdc: Log.Mdc) = f(self.error(msg, mdc))

def error(msg: => String, cause: Throwable, mdc: => Log.Mdc) = f(self.error(msg, cause, mdc))
def error(msg: => String, cause: Throwable, mdc: Log.Mdc) = f(self.error(msg, cause, mdc))
}

def mapMsg(f: String => String): Log[F] = new Log[F] {

def trace(msg: => String, mdc: => Log.Mdc) = self.trace(f(msg), mdc)
def trace(msg: => String, mdc: Log.Mdc) = self.trace(f(msg), mdc)

def debug(msg: => String, mdc: => Log.Mdc) = self.debug(f(msg), mdc)
def debug(msg: => String, mdc: Log.Mdc) = self.debug(f(msg), mdc)

def info(msg: => String, mdc: => Log.Mdc) = self.info(f(msg), mdc)
def info(msg: => String, mdc: Log.Mdc) = self.info(f(msg), mdc)

def warn(msg: => String, mdc: => Log.Mdc) = self.warn(f(msg), mdc)
def warn(msg: => String, mdc: Log.Mdc) = self.warn(f(msg), mdc)

def warn(msg: => String, cause: Throwable, mdc: => Log.Mdc) = self.warn(f(msg), cause, mdc)
def warn(msg: => String, cause: Throwable, mdc: Log.Mdc) = self.warn(f(msg), cause, mdc)

def error(msg: => String, mdc: => Log.Mdc) = self.error(f(msg), mdc)
def error(msg: => String, mdc: Log.Mdc) = self.error(f(msg), mdc)

def error(msg: => String, cause: Throwable, mdc: => Log.Mdc) = self.error(f(msg), cause, mdc)
def error(msg: => String, cause: Throwable, mdc: Log.Mdc) = self.error(f(msg), cause, mdc)
}

def prefixed(prefix: String): Log[F] = mapMsg(msg => s"$prefix $msg")
Expand Down
File renamed without changes.
44 changes: 11 additions & 33 deletions core/src/test/scala/com/evolutiongaming/catshelper/LogSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ import cats.effect.IO
import scala.util.control.NoStackTrace
import org.scalatest.funsuite.AnyFunSuite
import org.scalatest.matchers.should.Matchers
import com.evolutiongaming.catshelper.IOSuite.*

import scala.concurrent.duration.*
import com.evolutiongaming.catshelper.IOSuite._

class LogSpec extends AnyFunSuite with Matchers {

Expand Down Expand Up @@ -71,35 +69,15 @@ class LogSpec extends AnyFunSuite with Matchers {
Action.OfStr("source")))
}

test("MDC ThreadLocal cleanup") {
test("MDC cleanup") {

val io = for {
logOf <- LogOf.slf4j[IO]
log <- logOf(getClass)
_ <- log.info("whatever", Log.Mdc("k" -> "v"))
} yield {
val context = org.slf4j.MDC.getCopyOfContextMap
context shouldEqual null
}

io.unsafeRunSync()
}

test("MDC lazy evaluation") {
var used = false
def mdc = {
used = true
Log.Mdc.empty
}

val io = for {
log <- LogOf.slf4j[IO]
log <- log("test-lazy-mdc")
_ <- IO.sleep(1.second) // await for logger initialisation: https://www.slf4j.org/codes.html#replay
_ <- log.trace("should not appear in (debug) log", mdc) // trace is disabled in logback-text.xml
} yield used shouldEqual false
} yield org.slf4j.MDC.getCopyOfContextMap

io.unsafeRunSync()
io.unsafeRunSync() shouldEqual null
}

test("LogOf.log") {
Expand Down Expand Up @@ -139,49 +117,49 @@ object LogSpec {
val log: Log[StateT] = {
val log = new Log[StateT] {

def trace(msg: => String, mdc: => Log.Mdc) = {
def trace(msg: => String, mdc: Log.Mdc) = {
StateT { state =>
val action = Action.Trace(msg, mdc)
(state.add(action), ())
}
}

def debug(msg: => String, mdc: => Log.Mdc) = {
def debug(msg: => String, mdc: Log.Mdc) = {
StateT { state =>
val action = Action.Debug(msg, mdc)
(state.add(action), ())
}
}

def info(msg: => String, mdc: => Log.Mdc) = {
def info(msg: => String, mdc: Log.Mdc) = {
StateT { state =>
val action = Action.Info(msg, mdc)
(state.add(action), ())
}
}

def warn(msg: => String, mdc: => Log.Mdc) = {
def warn(msg: => String, mdc: Log.Mdc) = {
StateT { state =>
val action = Action.Warn0(msg, mdc)
(state.add(action), ())
}
}

def warn(msg: => String, cause: Throwable, mdc: => Log.Mdc) = {
def warn(msg: => String, cause: Throwable, mdc: Log.Mdc) = {
StateT { state =>
val action = Action.Warn1(msg, cause, mdc)
(state.add(action), ())
}
}

def error(msg: => String, mdc: => Log.Mdc) = {
def error(msg: => String, mdc: Log.Mdc) = {
StateT { state =>
val action = Action.Error0(msg, mdc)
(state.add(action), ())
}
}

def error(msg: => String, cause: Throwable, mdc: => Log.Mdc) = {
def error(msg: => String, cause: Throwable, mdc: Log.Mdc) = {
StateT { state =>
val action = Action.Error1(msg, cause, mdc)
(state.add(action), ())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import ch.qos.logback.classic.spi.LoggingEvent
import ch.qos.logback.classic.util.ContextInitializer
import com.evolutiongaming.catshelper.Log.Mdc

import scala.collection.JavaConverters.*
import scala.collection.JavaConverters._

/**
* ===Motivation===
Expand All @@ -31,7 +31,7 @@ object LogOfFromLogback {
log(context.getLogger(source))
}

def apply(source: Class[?]): F[Log[F]] =
def apply(source: Class[_]): F[Log[F]] =
apply(source.getName.stripSuffix("$"))
}
}
Expand All @@ -42,7 +42,7 @@ object LogOfFromLogback {
val FQCN = getClass.getName

def append(msg: => String,
mdc: => Mdc,
mdc: Mdc,
level: Level,
throwable: Throwable = null): F[Unit] = Sync[F].delay {
if (logger.isEnabledFor(level)) {
Expand All @@ -55,25 +55,25 @@ object LogOfFromLogback {
}
}

def trace(msg: => String, mdc: => Mdc): F[Unit] =
def trace(msg: => String, mdc: Mdc): F[Unit] =
append(msg, mdc, Level.TRACE)

def debug(msg: => String, mdc: => Mdc): F[Unit] =
def debug(msg: => String, mdc: Mdc): F[Unit] =
append(msg, mdc, Level.DEBUG)

def info(msg: => String, mdc: => Mdc): F[Unit] =
def info(msg: => String, mdc: Mdc): F[Unit] =
append(msg, mdc, Level.INFO)

def warn(msg: => String, mdc: => Mdc): F[Unit] =
def warn(msg: => String, mdc: Mdc): F[Unit] =
append(msg, mdc, Level.WARN)

def warn(msg: => String, cause: Throwable, mdc: => Mdc): F[Unit] =
def warn(msg: => String, cause: Throwable, mdc: Mdc): F[Unit] =
append(msg, mdc, Level.WARN, cause)

def error(msg: => String, mdc: => Mdc): F[Unit] =
def error(msg: => String, mdc: Mdc): F[Unit] =
append(msg, mdc, Level.ERROR)

def error(msg: => String, cause: Throwable, mdc: => Mdc): F[Unit] =
def error(msg: => String, cause: Throwable, mdc: Mdc): F[Unit] =
append(msg, mdc, Level.ERROR, cause)
}

Expand Down
13 changes: 13 additions & 0 deletions logback/src/test/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8" ?>

<configuration debug="true">
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] K:%X{k} %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>

<root level="debug">
<appender-ref ref="STDOUT"/>
</root>
</configuration>
2 changes: 1 addition & 1 deletion version.sbt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ThisBuild / version := "3.8.1-SNAPSHOT"
ThisBuild / version := "3.7.1-SNAPSHOT"

0 comments on commit b7ee222

Please sign in to comment.