From 1206d714a4cc8d0235391424cfb12f2e49dd59ee Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Fri, 10 Nov 2023 19:59:59 +0100 Subject: [PATCH] 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 --- .github/workflows/build-test-prValidation.yml | 3 +++ .../typed/internal/StubbedActorContext.scala | 23 +++++++++++++++++-- build.sbt | 1 + project/Dependencies.scala | 8 +++++++ 4 files changed, 33 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build-test-prValidation.yml b/.github/workflows/build-test-prValidation.yml index 314ff19d054..b3dfebb95cd 100644 --- a/.github/workflows/build-test-prValidation.yml +++ b/.github/workflows/build-test-prValidation.yml @@ -90,3 +90,6 @@ jobs: -Dsbt.log.noformat=false \ -Dpekko.log.timestamps=true \ validatePullRequest + + - name: sbt actor-typed-tests/test (with slf4j2) + run: sbt -Dpekko.test.slf4j2=true actor-typed-tests/test diff --git a/actor-testkit-typed/src/main/scala/org/apache/pekko/actor/testkit/typed/internal/StubbedActorContext.scala b/actor-testkit-typed/src/main/scala/org/apache/pekko/actor/testkit/typed/internal/StubbedActorContext.scala index 2f04ab5e59c..0348fc74ee0 100644 --- a/actor-testkit-typed/src/main/scala/org/apache/pekko/actor/testkit/typed/internal/StubbedActorContext.scala +++ b/actor-testkit-typed/src/main/scala/org/apache/pekko/actor/testkit/typed/internal/StubbedActorContext.scala @@ -21,7 +21,8 @@ import pekko.actor.{ ActorPath, ActorRefProvider, InvalidMessageException } import pekko.annotation.InternalApi import pekko.util.Helpers import pekko.{ actor => classic } -import org.slf4j.Logger +import org.slf4j.{ Logger, Marker } +import org.slf4j.event.SubstituteLoggingEvent import org.slf4j.helpers.{ MessageFormatter, SubstituteLoggerFactory } import java.util.concurrent.ThreadLocalRandom.{ current => rnd } @@ -243,15 +244,33 @@ private[pekko] final class FunctionRef[-T](override val path: ActorPath, send: ( .iterator() .asScala .map { evt => + val marker: Option[Marker] = + try { + Option(evt.getMarker) + } catch { + case _: NoSuchMethodError => + // evt.getMarker was replaced in slf4j v2 with evt.getMarkers + getFirstMarkerFromSlf4J2(evt) + } CapturedLogEvent( level = evt.getLevel, message = MessageFormatter.arrayFormat(evt.getMessage, evt.getArgumentArray).getMessage, cause = Option(evt.getThrowable), - marker = Option(evt.getMarker)) + marker = marker) } .toList } + private def getFirstMarkerFromSlf4J2(evt: SubstituteLoggingEvent): Option[Marker] = { + try { + val method = classOf[SubstituteLoggingEvent].getMethod("getMarkers") + val markers = method.invoke(evt).asInstanceOf[java.util.List[Marker]] + if (markers == null || markers.isEmpty) None else Some(markers.get(0)) + } catch { + case _: NoSuchMethodException => None + } + } + /** * Clear the log entries. */ diff --git a/build.sbt b/build.sbt index 3daf08f7989..a23dcd34369 100644 --- a/build.sbt +++ b/build.sbt @@ -586,6 +586,7 @@ lazy val actorTestkitTyped = pekkoModule("actor-testkit-typed") lazy val actorTypedTests = pekkoModule("actor-typed-tests") .dependsOn(actorTyped % "compile->CompileJdk9", actorTestkitTyped % "compile->compile;test->test", actor) .settings(PekkoBuild.mayChangeSettings) + .settings(Dependencies.actorTypedTestSlf4j2) .disablePlugins(MimaPlugin) .enablePlugins(NoPublish) diff --git a/project/Dependencies.scala b/project/Dependencies.scala index 4f455bc7d33..d11e82dbc7b 100644 --- a/project/Dependencies.scala +++ b/project/Dependencies.scala @@ -265,6 +265,14 @@ object Dependencies { Provided.scalatest.value, TestDependencies.scalatestJUnit.value) + val actorTypedTestSlf4j2 = if (java.lang.Boolean.getBoolean("pekko.test.slf4j2")) { + Seq(dependencyOverrides ++= Seq( + "org.slf4j" % "slf4j-api" % "2.0.9", + "ch.qos.logback" % "logback-classic" % "1.3.11" % Test)) + } else { + Seq.empty + } + val pki = l ++= Seq( asnOne,