Skip to content

Commit 3f890b0

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 8dc4e60 commit 3f890b0

File tree

16 files changed

+1196
-37
lines changed

16 files changed

+1196
-37
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.11", binary-version: "2.13", java-version: "11", java-distribution: "temurin", report: "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

.github/workflows/coverage.yml

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

2626
steps:
2727
- 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: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ lazy val loggerF = (project in file("."))
8484
slf4jMdcJvm,
8585
logbackMdcMonix3Jvm,
8686
testLogbackMdcMonix3Jvm,
87+
logbackMdcCatsEffect3Jvm,
8788
testKitJvm,
8889
// testKitJs,
8990
catsEffectJvm,
@@ -110,7 +111,7 @@ lazy val core =
110111
),
111112
)
112113
lazy val coreJvm = core.jvm
113-
lazy val coreJs = core.js
114+
lazy val coreJs = core.js.settings(commonJsSettings)
114115

115116
lazy val slf4jLogger = module(ProjectName("slf4j"), crossProject(JVMPlatform, JSPlatform))
116117
.settings(
@@ -125,7 +126,7 @@ lazy val slf4jLogger = module(ProjectName("slf4j"), crossProject(JVMPlatform,
125126
)
126127
.dependsOn(core)
127128
lazy val slf4jLoggerJvm = slf4jLogger.jvm
128-
lazy val slf4jLoggerJs = slf4jLogger.js
129+
lazy val slf4jLoggerJs = slf4jLogger.js.settings(commonJsSettings)
129130

130131
lazy val log4sLogger =
131132
module(ProjectName("log4s"), crossProject(JVMPlatform, JSPlatform))
@@ -141,7 +142,7 @@ lazy val log4sLogger =
141142
)
142143
.dependsOn(core)
143144
lazy val log4sLoggerJvm = log4sLogger.jvm
144-
lazy val log4sLoggerJs = log4sLogger.js
145+
lazy val log4sLoggerJs = log4sLogger.js.settings(commonJsSettings)
145146

146147
lazy val log4jLogger =
147148
module(ProjectName("log4j"), crossProject(JVMPlatform, JSPlatform))
@@ -201,7 +202,7 @@ lazy val log4jLogger =
201202
)
202203
.dependsOn(core)
203204
lazy val log4jLoggerJvm = log4jLogger.jvm
204-
lazy val log4jLoggerJs = log4jLogger.js
205+
lazy val log4jLoggerJs = log4jLogger.js.settings(commonJsSettings)
205206

206207
lazy val sbtLogging =
207208
module(ProjectName("sbt-logging"), crossProject(JVMPlatform, JSPlatform))
@@ -233,7 +234,7 @@ lazy val sbtLogging =
233234
)
234235
.dependsOn(core)
235236
lazy val sbtLoggingJvm = sbtLogging.jvm
236-
lazy val sbtLoggingJs = sbtLogging.js
237+
lazy val sbtLoggingJs = sbtLogging.js.settings(commonJsSettings)
237238

238239
lazy val cats =
239240
module(ProjectName("cats"), crossProject(JVMPlatform, JSPlatform))
@@ -252,7 +253,7 @@ lazy val cats =
252253
)
253254
.dependsOn(core % props.IncludeTest)
254255
lazy val catsJvm = cats.jvm
255-
lazy val catsJs = cats.js
256+
lazy val catsJs = cats.js.settings(commonJsSettings)
256257

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

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

341365
lazy val catsEffect =
342366
module(ProjectName("cats-effect"), crossProject(JVMPlatform, JSPlatform))
@@ -351,7 +375,7 @@ lazy val catsEffect =
351375
.settings(noPublish)
352376
.dependsOn(core % props.IncludeTest, cats)
353377
lazy val catsEffectJvm = catsEffect.jvm
354-
lazy val catsEffectJs = catsEffect.js
378+
lazy val catsEffectJs = catsEffect.js.settings(commonJsSettings)
355379

356380
lazy val catsEffect3 =
357381
module(ProjectName("cats-effect3"), crossProject(JVMPlatform, JSPlatform))
@@ -369,7 +393,7 @@ lazy val catsEffect3 =
369393
.settings(noPublish)
370394
.dependsOn(core % props.IncludeTest, cats)
371395
lazy val catsEffect3Jvm = catsEffect3.jvm
372-
lazy val catsEffect3Js = catsEffect3.js
396+
lazy val catsEffect3Js = catsEffect3.js.settings(commonJsSettings)
373397

374398
lazy val monix =
375399
module(ProjectName("monix"), crossProject(JVMPlatform, JSPlatform))
@@ -384,7 +408,7 @@ lazy val monix =
384408
.settings(noPublish)
385409
.dependsOn(core % props.IncludeTest, cats)
386410
lazy val monixJvm = monix.jvm
387-
lazy val monixJs = monix.js
411+
lazy val monixJs = monix.js.settings(commonJsSettings)
388412

389413
lazy val testCatsEffectWithSlf4jLogger =
390414
testProject(
@@ -558,7 +582,7 @@ lazy val props =
558582
final val GitHubUsername = "Kevin-Lee"
559583
final val RepoName = "logger-f"
560584

561-
final val Scala3Versions = List("3.0.2")
585+
final val Scala3Versions = List("3.3.0")
562586
final val Scala2Versions = List("2.13.11", "2.12.18")
563587

564588
// final val ProjectScalaVersion = Scala3Versions.head
@@ -571,8 +595,8 @@ lazy val props =
571595

572596
val removeDottyIncompatible: ModuleID => Boolean =
573597
m =>
574-
m.name == "wartremover" ||
575-
m.name == "ammonite" ||
598+
// m.name == "wartremover" ||
599+
m.name == "ammonite" ||
576600
m.name == "kind-projector" ||
577601
m.name == "better-monadic-for" ||
578602
m.name == "mdoc"
@@ -632,6 +656,8 @@ lazy val libs =
632656

633657
lazy val catsEffect3 = "org.typelevel" %% "cats-effect" % props.CatsEffect3Version
634658

659+
lazy val catsEffect3Eap = "org.typelevel" %% "cats-effect" % "3.6-02a43a6"
660+
635661
lazy val monix3Execution = "io.monix" %% "monix-execution" % props.Monix3Version
636662

637663
lazy val effectieCore: ModuleID = "io.kevinlee" %% "effectie-core" % props.EffectieVersion
@@ -664,6 +690,8 @@ lazy val libs =
664690

665691
lazy val extrasCats = "io.kevinlee" %% "extras-cats" % props.ExtrasVersion % Test
666692

693+
lazy val effectieCatsEffect3 = "io.kevinlee" %% "effectie-cats-effect3" % props.EffectieVersion
694+
667695
lazy val extrasConcurrent = "io.kevinlee" %% "extras-concurrent" % props.ExtrasVersion % Test
668696
lazy val extrasConcurrentTesting = "io.kevinlee" %% "extras-concurrent-testing" % props.ExtrasVersion % Test
669697

@@ -733,6 +761,7 @@ def projectCommonSettings(projectName: String, crossProject: CrossProject.Builde
733761
// , Compile / compile / wartremoverErrors ++= commonWarts((update / scalaBinaryVersion).value)
734762
// , Test / compile / wartremoverErrors ++= commonWarts((update / scalaBinaryVersion).value)
735763
wartremoverErrors ++= commonWarts((update / scalaBinaryVersion).value),
764+
fork := true,
736765
Compile / console / wartremoverErrors := List.empty,
737766
Compile / console / wartremoverWarnings := List.empty,
738767
Compile / console / scalacOptions :=
@@ -759,3 +788,11 @@ def projectCommonSettings(projectName: String, crossProject: CrossProject.Builde
759788
.settings(
760789
mavenCentralPublishSettings
761790
)
791+
792+
lazy val commonJsSettings: SettingsDefinition = List(
793+
Test / fork := false
794+
// Test / scalacOptions ++= (if (scalaVersion.value.startsWith("3")) List.empty
795+
// else List("-P:scalajs:nowarnGlobalExecutionContext")),
796+
// Test / compile / scalacOptions ++= (if (scalaVersion.value.startsWith("3")) List.empty
797+
// else List("-P:scalajs:nowarnGlobalExecutionContext")),
798+
)

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)