Skip to content

Commit 980bc84

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 a0f7da9 commit 980bc84

File tree

16 files changed

+1197
-36
lines changed

16 files changed

+1197
-36
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
scala:
2222
- { name: "Scala 2", version: "2.12.18", binary-version: "2.12", java-version: "11", java-distribution: "temurin", report: "" }
2323
- { name: "Scala 2", version: "2.13.11", binary-version: "2.13", java-version: "11", java-distribution: "temurin", report: "report" }
24-
- { name: "Scala 3", version: "3.0.2", binary-version: "3", java-version: "11", java-distribution: "temurin", report: "" }
24+
- { name: "Scala 3", version: "3.3.0", binary-version: "3", java-version: "11", java-distribution: "temurin", report: "" }
2525

2626
steps:
2727
- uses: actions/checkout@v4

.github/workflows/coverage.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
strategy:
1919
matrix:
2020
scala:
21-
- { name: "Scala 2", version: "2.13.11", binary-version: "2.13", java-version: "11", java-distribution: "temurin", report: "report" }
21+
- { name: "Scala 2", version: "2.13.11", binary-version: "2.13", java-version: "11", java-distribution: "temurin", report: "report" }
2222

2323
steps:
2424
- 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.11", binary-version: "2.13", java-version: "11", java-distribution: "temurin", report: "" }
27-
- { name: "Scala 3", version: "3.0.2", 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: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ lazy val loggerF = (project in file("."))
8383
// catsJs,
8484
logbackMdcMonix3Jvm,
8585
// logbackMdcMonix3Js,
86+
logbackMdcCatsEffect3Jvm,
87+
// logbackMdcCatsEffect3Js,
8688
testKitJvm,
8789
// testKitJs,
8890
catsEffectJvm,
@@ -109,7 +111,7 @@ lazy val core =
109111
),
110112
)
111113
lazy val coreJvm = core.jvm
112-
lazy val coreJs = core.js
114+
lazy val coreJs = core.js.settings(commonJsSettings)
113115

114116
lazy val slf4jLogger = module(ProjectName("slf4j"), crossProject(JVMPlatform, JSPlatform))
115117
.settings(
@@ -124,7 +126,7 @@ lazy val slf4jLogger = module(ProjectName("slf4j"), crossProject(JVMPlatform,
124126
)
125127
.dependsOn(core)
126128
lazy val slf4jLoggerJvm = slf4jLogger.jvm
127-
lazy val slf4jLoggerJs = slf4jLogger.js
129+
lazy val slf4jLoggerJs = slf4jLogger.js.settings(commonJsSettings)
128130

129131
lazy val log4sLogger =
130132
module(ProjectName("log4s"), crossProject(JVMPlatform, JSPlatform))
@@ -140,7 +142,7 @@ lazy val log4sLogger =
140142
)
141143
.dependsOn(core)
142144
lazy val log4sLoggerJvm = log4sLogger.jvm
143-
lazy val log4sLoggerJs = log4sLogger.js
145+
lazy val log4sLoggerJs = log4sLogger.js.settings(commonJsSettings)
144146

145147
lazy val log4jLogger =
146148
module(ProjectName("log4j"), crossProject(JVMPlatform, JSPlatform))
@@ -200,7 +202,7 @@ lazy val log4jLogger =
200202
)
201203
.dependsOn(core)
202204
lazy val log4jLoggerJvm = log4jLogger.jvm
203-
lazy val log4jLoggerJs = log4jLogger.js
205+
lazy val log4jLoggerJs = log4jLogger.js.settings(commonJsSettings)
204206

205207
lazy val sbtLogging =
206208
module(ProjectName("sbt-logging"), crossProject(JVMPlatform, JSPlatform))
@@ -232,7 +234,7 @@ lazy val sbtLogging =
232234
)
233235
.dependsOn(core)
234236
lazy val sbtLoggingJvm = sbtLogging.jvm
235-
lazy val sbtLoggingJs = sbtLogging.js
237+
lazy val sbtLoggingJs = sbtLogging.js.settings(commonJsSettings)
236238

237239
lazy val cats =
238240
module(ProjectName("cats"), crossProject(JVMPlatform, JSPlatform))
@@ -251,7 +253,7 @@ lazy val cats =
251253
)
252254
.dependsOn(core % props.IncludeTest)
253255
lazy val catsJvm = cats.jvm
254-
lazy val catsJs = cats.js
256+
lazy val catsJs = cats.js.settings(commonJsSettings)
255257

256258
lazy val logbackMdcMonix3 = module(ProjectName("logback-mdc-monix3"), crossProject(JVMPlatform, JSPlatform))
257259
.settings(
@@ -276,6 +278,30 @@ lazy val logbackMdcMonix3 = module(ProjectName("logback-mdc-monix3"), crossPr
276278
lazy val logbackMdcMonix3Jvm = logbackMdcMonix3.jvm
277279
lazy val logbackMdcMonix3Js = logbackMdcMonix3.js
278280

281+
lazy val logbackMdcCatsEffect3 = module(ProjectName("logback-mdc-cats-effect3"), crossProject(JVMPlatform, JSPlatform))
282+
.settings(
283+
description := "Logger for F[_] - logback MDC context map support for Cats Effect 3",
284+
libraryDependencies ++= Seq(
285+
libs.logbackClassic,
286+
libs.logbackScalaInterop,
287+
libs.catsEffect3Eap,
288+
libs.tests.effectieCatsEffect3,
289+
libs.tests.extrasHedgehogCatsEffect3,
290+
) ++ libs.tests.hedgehogLibs,
291+
libraryDependencies := libraryDependenciesRemoveScala3Incompatible(
292+
scalaVersion.value,
293+
libraryDependencies.value,
294+
),
295+
javaOptions += "-Dcats.effect.ioLocalPropagation=true",
296+
)
297+
.dependsOn(
298+
core,
299+
monix % Test,
300+
slf4jLogger % Test,
301+
)
302+
lazy val logbackMdcCatsEffect3Jvm = logbackMdcCatsEffect3.jvm
303+
lazy val logbackMdcCatsEffect3Js = logbackMdcCatsEffect3.js.settings(commonJsSettings)
304+
279305
lazy val testKit =
280306
module(ProjectName("test-kit"), crossProject(JVMPlatform, JSPlatform))
281307
.settings(
@@ -292,7 +318,7 @@ lazy val testKit =
292318
)
293319
.dependsOn(core % props.IncludeTest)
294320
lazy val testKitJvm = testKit.jvm
295-
lazy val testKitJs = testKit.js
321+
lazy val testKitJs = testKit.js.settings(commonJsSettings)
296322

297323
lazy val catsEffect =
298324
module(ProjectName("cats-effect"), crossProject(JVMPlatform, JSPlatform))
@@ -307,7 +333,7 @@ lazy val catsEffect =
307333
.settings(noPublish)
308334
.dependsOn(core % props.IncludeTest, cats)
309335
lazy val catsEffectJvm = catsEffect.jvm
310-
lazy val catsEffectJs = catsEffect.js
336+
lazy val catsEffectJs = catsEffect.js.settings(commonJsSettings)
311337

312338
lazy val catsEffect3 =
313339
module(ProjectName("cats-effect3"), crossProject(JVMPlatform, JSPlatform))
@@ -325,7 +351,7 @@ lazy val catsEffect3 =
325351
.settings(noPublish)
326352
.dependsOn(core % props.IncludeTest, cats)
327353
lazy val catsEffect3Jvm = catsEffect3.jvm
328-
lazy val catsEffect3Js = catsEffect3.js
354+
lazy val catsEffect3Js = catsEffect3.js.settings(commonJsSettings)
329355

330356
lazy val monix =
331357
module(ProjectName("monix"), crossProject(JVMPlatform, JSPlatform))
@@ -340,7 +366,7 @@ lazy val monix =
340366
.settings(noPublish)
341367
.dependsOn(core % props.IncludeTest, cats)
342368
lazy val monixJvm = monix.jvm
343-
lazy val monixJs = monix.js
369+
lazy val monixJs = monix.js.settings(commonJsSettings)
344370

345371
lazy val testCatsEffectWithSlf4jLogger =
346372
testProject(
@@ -514,7 +540,7 @@ lazy val props =
514540
final val GitHubUsername = "Kevin-Lee"
515541
final val RepoName = "logger-f"
516542

517-
final val Scala3Versions = List("3.0.2")
543+
final val Scala3Versions = List("3.3.0")
518544
final val Scala2Versions = List("2.13.11", "2.12.18")
519545

520546
// final val ProjectScalaVersion = Scala3Versions.head
@@ -527,7 +553,7 @@ lazy val props =
527553

528554
val removeDottyIncompatible: ModuleID => Boolean =
529555
m =>
530-
m.name == "wartremover" ||
556+
// m.name == "wartremover" ||
531557
m.name == "ammonite" ||
532558
m.name == "kind-projector" ||
533559
m.name == "better-monadic-for" ||
@@ -581,6 +607,8 @@ lazy val libs =
581607

582608
lazy val catsEffect3 = "org.typelevel" %% "cats-effect" % props.CatsEffect3Version
583609

610+
lazy val catsEffect3Eap = "org.typelevel" %% "cats-effect" % "3.6-02a43a6"
611+
584612
lazy val monix3Execution = "io.monix" %% "monix-execution" % props.Monix3Version
585613

586614
lazy val effectieCore: ModuleID = "io.kevinlee" %% "effectie-core" % props.EffectieVersion
@@ -611,6 +639,8 @@ lazy val libs =
611639

612640
lazy val extrasCats = "io.kevinlee" %% "extras-cats" % props.ExtrasVersion % Test
613641

642+
lazy val effectieCatsEffect3 = "io.kevinlee" %% "effectie-cats-effect3" % props.EffectieVersion
643+
614644
lazy val extrasConcurrent = "io.kevinlee" %% "extras-concurrent" % props.ExtrasVersion % Test
615645
lazy val extrasConcurrentTesting = "io.kevinlee" %% "extras-concurrent-testing" % props.ExtrasVersion % Test
616646

@@ -667,6 +697,7 @@ def projectCommonSettings(projectName: String, crossProject: CrossProject.Builde
667697
// , Compile / compile / wartremoverErrors ++= commonWarts((update / scalaBinaryVersion).value)
668698
// , Test / compile / wartremoverErrors ++= commonWarts((update / scalaBinaryVersion).value)
669699
wartremoverErrors ++= commonWarts((update / scalaBinaryVersion).value),
700+
fork := true,
670701
Compile / console / wartremoverErrors := List.empty,
671702
Compile / console / wartremoverWarnings := List.empty,
672703
Compile / console / scalacOptions :=
@@ -693,3 +724,11 @@ def projectCommonSettings(projectName: String, crossProject: CrossProject.Builde
693724
.settings(
694725
mavenCentralPublishSettings
695726
)
727+
728+
lazy val commonJsSettings: SettingsDefinition = List(
729+
Test / fork := false,
730+
// Test / scalacOptions ++= (if (scalaVersion.value.startsWith("3")) List.empty
731+
// else List("-P:scalajs:nowarnGlobalExecutionContext")),
732+
// Test / compile / scalacOptions ++= (if (scalaVersion.value.startsWith("3")) List.empty
733+
// else List("-P:scalajs:nowarnGlobalExecutionContext")),
734+
)

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)