Skip to content

Commit b1cda8a

Browse files
committed
Close #441 - Add MdcAdapter for Cats Effect 3 to properly share context through MDC with IO and IOLocal from Cats Effect 3
1 parent ba7da9b commit b1cda8a

File tree

15 files changed

+1186
-27
lines changed

15 files changed

+1186
-27
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
scala:
2525
- { name: "Scala 2", version: "2.12.18", binary-version: "2.12", java-version: "11", java-distribution: "temurin", report: "" }
2626
- { name: "Scala 2", version: "2.13.14", binary-version: "2.13", java-version: "11", java-distribution: "temurin", report: "report" }
27-
- { name: "Scala 3", version: "3.1.3", binary-version: "3", java-version: "11", java-distribution: "temurin", report: "" }
27+
- { name: "Scala 3", version: "3.3.0", binary-version: "3", java-version: "11", java-distribution: "temurin", report: "" }
2828

2929
steps:
3030
- uses: actions/checkout@v4

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
scala:
2525
- { name: "Scala 2", version: "2.12.18", binary-version: "2.12", java-version: "11", java-distribution: "temurin", report: "" }
2626
- { name: "Scala 2", version: "2.13.14", binary-version: "2.13", java-version: "11", java-distribution: "temurin", report: "" }
27-
- { name: "Scala 3", version: "3.1.3", binary-version: "3", java-version: "11", java-distribution: "temurin", report: "" }
27+
- { name: "Scala 3", version: "3.3.0", binary-version: "3", java-version: "11", java-distribution: "temurin", report: "" }
2828

2929
steps:
3030
- uses: actions/checkout@v4

build.sbt

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ lazy val loggerF = (project in file("."))
8181
slf4jMdcJvm,
8282
logbackMdcMonix3Jvm,
8383
testLogbackMdcMonix3Jvm,
84+
logbackMdcCatsEffect3Jvm,
8485
testKitJvm,
8586
// testKitJs,
8687
catsEffectJvm,
@@ -109,7 +110,7 @@ lazy val core =
109110
),
110111
)
111112
lazy val coreJvm = core.jvm
112-
lazy val coreJs = core.js
113+
lazy val coreJs = core.js.settings(commonJsSettings)
113114

114115
lazy val slf4jLogger = module(ProjectName("slf4j"), crossProject(JVMPlatform, JSPlatform))
115116
.settings(
@@ -124,7 +125,7 @@ lazy val slf4jLogger = module(ProjectName("slf4j"), crossProject(JVMPlatform,
124125
)
125126
.dependsOn(core)
126127
lazy val slf4jLoggerJvm = slf4jLogger.jvm
127-
lazy val slf4jLoggerJs = slf4jLogger.js
128+
lazy val slf4jLoggerJs = slf4jLogger.js.settings(commonJsSettings)
128129

129130
lazy val log4sLogger =
130131
module(ProjectName("log4s"), crossProject(JVMPlatform, JSPlatform))
@@ -140,7 +141,7 @@ lazy val log4sLogger =
140141
)
141142
.dependsOn(core)
142143
lazy val log4sLoggerJvm = log4sLogger.jvm
143-
lazy val log4sLoggerJs = log4sLogger.js
144+
lazy val log4sLoggerJs = log4sLogger.js.settings(commonJsSettings)
144145

145146
lazy val log4jLogger =
146147
module(ProjectName("log4j"), crossProject(JVMPlatform, JSPlatform))
@@ -200,7 +201,7 @@ lazy val log4jLogger =
200201
)
201202
.dependsOn(core)
202203
lazy val log4jLoggerJvm = log4jLogger.jvm
203-
lazy val log4jLoggerJs = log4jLogger.js
204+
lazy val log4jLoggerJs = log4jLogger.js.settings(commonJsSettings)
204205

205206
lazy val sbtLogging =
206207
module(ProjectName("sbt-logging"), crossProject(JVMPlatform, JSPlatform))
@@ -232,7 +233,7 @@ lazy val sbtLogging =
232233
)
233234
.dependsOn(core)
234235
lazy val sbtLoggingJvm = sbtLogging.jvm
235-
lazy val sbtLoggingJs = sbtLogging.js
236+
lazy val sbtLoggingJs = sbtLogging.js.settings(commonJsSettings)
236237

237238
lazy val cats =
238239
module(ProjectName("cats"), crossProject(JVMPlatform, JSPlatform))
@@ -251,7 +252,7 @@ lazy val cats =
251252
)
252253
.dependsOn(core % props.IncludeTest)
253254
lazy val catsJvm = cats.jvm
254-
lazy val catsJs = cats.js
255+
lazy val catsJs = cats.js.settings(commonJsSettings)
255256

256257
lazy val slf4jMdc = module(ProjectName("slf4j-mdc"), crossProject(JVMPlatform))
257258
.settings(
@@ -319,6 +320,29 @@ lazy val testLogbackMdcMonix3 = testProject(ProjectName("logback-mdc-monix3")
319320
)
320321
lazy val testLogbackMdcMonix3Jvm = testLogbackMdcMonix3.jvm
321322

323+
lazy val logbackMdcCatsEffect3 = module(ProjectName("logback-mdc-cats-effect3"), crossProject(JVMPlatform))
324+
.settings(
325+
description := "Logger for F[_] - logback MDC context map support for Cats Effect 3",
326+
libraryDependencies ++= Seq(
327+
libs.logbackClassic,
328+
libs.logbackScalaInterop,
329+
libs.catsEffect3Eap,
330+
libs.tests.effectieCatsEffect3,
331+
libs.tests.extrasHedgehogCatsEffect3,
332+
) ++ libs.tests.hedgehogLibs,
333+
libraryDependencies := libraryDependenciesRemoveScala3Incompatible(
334+
scalaVersion.value,
335+
libraryDependencies.value,
336+
),
337+
javaOptions += "-Dcats.effect.ioLocalPropagation=true",
338+
)
339+
.dependsOn(
340+
core,
341+
monix % Test,
342+
slf4jLogger % Test,
343+
)
344+
lazy val logbackMdcCatsEffect3Jvm = logbackMdcCatsEffect3.jvm
345+
322346
lazy val testKit =
323347
module(ProjectName("test-kit"), crossProject(JVMPlatform, JSPlatform))
324348
.settings(
@@ -335,7 +359,7 @@ lazy val testKit =
335359
)
336360
.dependsOn(core % props.IncludeTest)
337361
lazy val testKitJvm = testKit.jvm
338-
lazy val testKitJs = testKit.js
362+
lazy val testKitJs = testKit.js.settings(commonJsSettings)
339363

340364
lazy val catsEffect =
341365
module(ProjectName("cats-effect"), crossProject(JVMPlatform, JSPlatform))
@@ -350,14 +374,14 @@ lazy val catsEffect =
350374
.settings(noPublish)
351375
.dependsOn(core % props.IncludeTest, cats)
352376
lazy val catsEffectJvm = catsEffect.jvm
353-
lazy val catsEffectJs = catsEffect.js
377+
lazy val catsEffectJs = catsEffect.js.settings(commonJsSettings)
354378

355379
lazy val catsEffect3 =
356380
module(ProjectName("cats-effect3"), crossProject(JVMPlatform, JSPlatform))
357381
.settings(
358382
description := "Logger for F[_] - Cats Effect 3",
359383
libraryDependencies ++= libs.tests.hedgehogLibs ++ List(
360-
libs.effectieCatsEffect3 % Test,
384+
libs.tests.effectieCatsEffect3,
361385
libs.tests.extrasHedgehogCatsEffect3,
362386
),
363387
libraryDependencies := libraryDependenciesRemoveScala3Incompatible(
@@ -368,7 +392,7 @@ lazy val catsEffect3 =
368392
.settings(noPublish)
369393
.dependsOn(core % props.IncludeTest, cats)
370394
lazy val catsEffect3Jvm = catsEffect3.jvm
371-
lazy val catsEffect3Js = catsEffect3.js
395+
lazy val catsEffect3Js = catsEffect3.js.settings(commonJsSettings)
372396

373397
lazy val monix =
374398
module(ProjectName("monix"), crossProject(JVMPlatform, JSPlatform))
@@ -383,7 +407,7 @@ lazy val monix =
383407
.settings(noPublish)
384408
.dependsOn(core % props.IncludeTest, cats)
385409
lazy val monixJvm = monix.jvm
386-
lazy val monixJs = monix.js
410+
lazy val monixJs = monix.js.settings(commonJsSettings)
387411

388412
lazy val testCore =
389413
testProject(
@@ -601,7 +625,7 @@ lazy val props =
601625
final val GitHubUsername = "Kevin-Lee"
602626
final val RepoName = "logger-f"
603627

604-
final val Scala3Versions = List("3.1.3")
628+
final val Scala3Versions = List("3.3.0")
605629
final val Scala2Versions = List("2.13.14", "2.12.18")
606630

607631
// final val ProjectScalaVersion = Scala3Versions.head
@@ -673,6 +697,8 @@ lazy val libs =
673697

674698
lazy val catsEffect3 = "org.typelevel" %% "cats-effect" % props.CatsEffect3Version
675699

700+
lazy val catsEffect3Eap = "org.typelevel" %% "cats-effect" % "3.6-02a43a6"
701+
676702
lazy val monix3Execution = "io.monix" %% "monix-execution" % props.Monix3Version
677703

678704
lazy val effectieCore: ModuleID = "io.kevinlee" %% "effectie-core" % props.EffectieVersion
@@ -709,6 +735,8 @@ lazy val libs =
709735

710736
lazy val extrasTestingTools = "io.kevinlee" %% "extras-testing-tools" % props.ExtrasVersion % Test
711737

738+
lazy val effectieCatsEffect3 = "io.kevinlee" %% "effectie-cats-effect3" % props.EffectieVersion % Test
739+
712740
lazy val extrasConcurrent = "io.kevinlee" %% "extras-concurrent" % props.ExtrasVersion % Test
713741
lazy val extrasConcurrentTesting = "io.kevinlee" %% "extras-concurrent-testing" % props.ExtrasVersion % Test
714742

@@ -771,6 +799,7 @@ def projectCommonSettings(projectName: String, crossProject: CrossProject.Builde
771799
// , Compile / compile / wartremoverErrors ++= commonWarts((update / scalaBinaryVersion).value)
772800
// , Test / compile / wartremoverErrors ++= commonWarts((update / scalaBinaryVersion).value)
773801
wartremoverErrors ++= commonWarts((update / scalaBinaryVersion).value),
802+
fork := true,
774803
Compile / console / wartremoverErrors := List.empty,
775804
Compile / console / wartremoverWarnings := List.empty,
776805
Compile / console / scalacOptions :=
@@ -794,3 +823,11 @@ def projectCommonSettings(projectName: String, crossProject: CrossProject.Builde
794823
}),
795824
/* } Coveralls */
796825
)
826+
827+
lazy val commonJsSettings: SettingsDefinition = List(
828+
Test / fork := false
829+
// Test / scalacOptions ++= (if (scalaVersion.value.startsWith("3")) List.empty
830+
// else List("-P:scalajs:nowarnGlobalExecutionContext")),
831+
// Test / compile / scalacOptions ++= (if (scalaVersion.value.startsWith("3")) List.empty
832+
// else List("-P:scalajs:nowarnGlobalExecutionContext")),
833+
)

modules/logger-f-cats-effect3/shared/src/test/scala/loggerf/cats/testing/ConcurrentSupport.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ trait ConcurrentSupport {
2525
val stringWriter = new StringWriter()
2626
val printWriter = new PrintWriter(stringWriter)
2727
th.printStackTrace(printWriter)
28-
logger(s"⚠️ Error in Executor: ${stringWriter.toString}")
28+
29+
@SuppressWarnings(Array("org.wartremover.warts.ToString"))
30+
val message = stringWriter.toString
31+
logger(s"⚠️ Error in Executor: $message")
2932
},
3033
)
3134

modules/logger-f-cats/shared/src/main/scala-3/loggerf/instances/show.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import loggerf.core.ToLog
77
* @since 2022-02-19
88
*/
99
trait show {
10-
inline given showToLog[A: Show]: ToLog[A] = Show[A].show(_)
10+
given showToLog[A: Show]: ToLog[A] with {
11+
inline def toLogMessage(a: A): String = Show[A].show(a)
12+
}
1113
}
1214

1315
object show extends show

modules/logger-f-core/shared/src/main/scala-3/loggerf/core/ToLog.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,7 @@ object ToLog {
1515
@SuppressWarnings(Array("org.wartremover.warts.ToString"))
1616
def fromToString[A]: ToLog[A] = _.toString
1717

18-
inline given stringToLog: ToLog[String] = identity(_)
18+
given stringToLog: ToLog[String] with {
19+
inline def toLogMessage(a: String): String = a
20+
}
1921
}

modules/logger-f-core/shared/src/main/scala-3/loggerf/core/syntax/LogSyntax.scala

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ package loggerf.core.syntax
33
import loggerf.LeveledMessage
44
import loggerf.Ignore
55
import loggerf.core.Log
6-
import loggerf.instances.future.given
7-
8-
import scala.concurrent.Future
96

107
/** @author Kevin Lee
118
* @since 2022-02-09
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package loggerf.logger.logback
2+
3+
import cats.effect.unsafe.IOLocals
4+
import cats.effect.{IOLocal, SyncIO}
5+
import cats.syntax.all._
6+
import ch.qos.logback.classic.LoggerContext
7+
import logback_scala_interop.JLoggerFMdcAdapter
8+
import org.slf4j.{LoggerFactory, MDC}
9+
10+
import java.util.{Map => JMap, Set => JSet}
11+
import scala.jdk.CollectionConverters._
12+
import scala.util.control.NonFatal
13+
14+
/** @author Kevin Lee
15+
* @since 2023-07-07
16+
*/
17+
class Ce3MdcAdapter extends JLoggerFMdcAdapter {
18+
19+
private[this] val localContext: IOLocal[Map[String, String]] =
20+
IOLocal[Map[String, String]](Map.empty[String, String])
21+
.syncStep(1)
22+
.flatMap(
23+
_.leftMap(_ =>
24+
new Error(
25+
"Failed to initialize the local context of the Ce3MdcAdapter."
26+
)
27+
).liftTo[SyncIO]
28+
)
29+
.unsafeRunSync()
30+
31+
override def put(key: String, `val`: String): Unit =
32+
IOLocals.update(localContext)(_ + (key -> `val`))
33+
34+
@SuppressWarnings(Array("org.wartremover.warts.Null"))
35+
override def get(key: String): String =
36+
IOLocals.get(localContext).getOrElse(key, null) // scalafix:ok DisableSyntax.null
37+
38+
override def remove(key: String): Unit = IOLocals.update(localContext)(_ - key)
39+
40+
override def clear(): Unit = IOLocals.reset(localContext)
41+
42+
override def getCopyOfContextMap: JMap[String, String] = getPropertyMap0
43+
44+
override def setContextMap0(contextMap: JMap[String, String]): Unit =
45+
IOLocals.set(localContext, contextMap.asScala.toMap)
46+
47+
private def getPropertyMap0: JMap[String, String] = IOLocals.get(localContext).asJava
48+
49+
override def getPropertyMap: JMap[String, String] = getPropertyMap0
50+
51+
override def getKeys: JSet[String] = IOLocals.get(localContext).keySet.asJava
52+
53+
}
54+
object Ce3MdcAdapter {
55+
56+
@SuppressWarnings(Array("org.wartremover.warts.Null"))
57+
private def initialize0(): Ce3MdcAdapter = {
58+
val field = classOf[MDC].getDeclaredField("mdcAdapter")
59+
field.setAccessible(true)
60+
val adapter = new Ce3MdcAdapter
61+
field.set(null, adapter) // scalafix:ok DisableSyntax.null
62+
field.setAccessible(false)
63+
adapter
64+
}
65+
66+
@SuppressWarnings(Array("org.wartremover.warts.AsInstanceOf", "scalafix:DisableSyntax.asInstanceOf"))
67+
def initialize(): Ce3MdcAdapter = {
68+
val loggerContext =
69+
LoggerFactory.getILoggerFactory.asInstanceOf[LoggerContext]
70+
initializeWithLoggerContext(loggerContext)
71+
}
72+
73+
def initializeWithLoggerContext(loggerContext: LoggerContext): Ce3MdcAdapter = {
74+
val adapter = initialize0()
75+
try {
76+
val field = classOf[LoggerContext].getDeclaredField("mdcAdapter")
77+
field.setAccessible(true)
78+
field.set(loggerContext, adapter)
79+
field.setAccessible(false)
80+
adapter
81+
} catch {
82+
case NonFatal(_) => adapter
83+
}
84+
}
85+
}

0 commit comments

Comments
 (0)