Skip to content

Commit 1206d71

Browse files
authored
small change to allow pekko-actor-testkit-typed to work with slf4j-api v2 (#784)
* small change to allow pekko-actor-testkit-typed to work with slf4j-api v2 * try to support testing with slf4j2 * Update Dependencies.scala * test with slf4j2 * use reflection to get first marker * SubstituteLoggingEvent.getMarkers can return null
1 parent f13601d commit 1206d71

File tree

4 files changed

+33
-2
lines changed

4 files changed

+33
-2
lines changed

.github/workflows/build-test-prValidation.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,6 @@ jobs:
9090
-Dsbt.log.noformat=false \
9191
-Dpekko.log.timestamps=true \
9292
validatePullRequest
93+
94+
- name: sbt actor-typed-tests/test (with slf4j2)
95+
run: sbt -Dpekko.test.slf4j2=true actor-typed-tests/test

actor-testkit-typed/src/main/scala/org/apache/pekko/actor/testkit/typed/internal/StubbedActorContext.scala

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ import pekko.actor.{ ActorPath, ActorRefProvider, InvalidMessageException }
2121
import pekko.annotation.InternalApi
2222
import pekko.util.Helpers
2323
import pekko.{ actor => classic }
24-
import org.slf4j.Logger
24+
import org.slf4j.{ Logger, Marker }
25+
import org.slf4j.event.SubstituteLoggingEvent
2526
import org.slf4j.helpers.{ MessageFormatter, SubstituteLoggerFactory }
2627

2728
import java.util.concurrent.ThreadLocalRandom.{ current => rnd }
@@ -243,15 +244,33 @@ private[pekko] final class FunctionRef[-T](override val path: ActorPath, send: (
243244
.iterator()
244245
.asScala
245246
.map { evt =>
247+
val marker: Option[Marker] =
248+
try {
249+
Option(evt.getMarker)
250+
} catch {
251+
case _: NoSuchMethodError =>
252+
// evt.getMarker was replaced in slf4j v2 with evt.getMarkers
253+
getFirstMarkerFromSlf4J2(evt)
254+
}
246255
CapturedLogEvent(
247256
level = evt.getLevel,
248257
message = MessageFormatter.arrayFormat(evt.getMessage, evt.getArgumentArray).getMessage,
249258
cause = Option(evt.getThrowable),
250-
marker = Option(evt.getMarker))
259+
marker = marker)
251260
}
252261
.toList
253262
}
254263

264+
private def getFirstMarkerFromSlf4J2(evt: SubstituteLoggingEvent): Option[Marker] = {
265+
try {
266+
val method = classOf[SubstituteLoggingEvent].getMethod("getMarkers")
267+
val markers = method.invoke(evt).asInstanceOf[java.util.List[Marker]]
268+
if (markers == null || markers.isEmpty) None else Some(markers.get(0))
269+
} catch {
270+
case _: NoSuchMethodException => None
271+
}
272+
}
273+
255274
/**
256275
* Clear the log entries.
257276
*/

build.sbt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,7 @@ lazy val actorTestkitTyped = pekkoModule("actor-testkit-typed")
586586
lazy val actorTypedTests = pekkoModule("actor-typed-tests")
587587
.dependsOn(actorTyped % "compile->CompileJdk9", actorTestkitTyped % "compile->compile;test->test", actor)
588588
.settings(PekkoBuild.mayChangeSettings)
589+
.settings(Dependencies.actorTypedTestSlf4j2)
589590
.disablePlugins(MimaPlugin)
590591
.enablePlugins(NoPublish)
591592

project/Dependencies.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,14 @@ object Dependencies {
265265
Provided.scalatest.value,
266266
TestDependencies.scalatestJUnit.value)
267267

268+
val actorTypedTestSlf4j2 = if (java.lang.Boolean.getBoolean("pekko.test.slf4j2")) {
269+
Seq(dependencyOverrides ++= Seq(
270+
"org.slf4j" % "slf4j-api" % "2.0.9",
271+
"ch.qos.logback" % "logback-classic" % "1.3.11" % Test))
272+
} else {
273+
Seq.empty
274+
}
275+
268276
val pki = l ++=
269277
Seq(
270278
asnOne,

0 commit comments

Comments
 (0)