Skip to content

Commit

Permalink
small change to allow pekko-actor-testkit-typed to work with slf4j-ap…
Browse files Browse the repository at this point in the history
…i v2 (apache#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
  • Loading branch information
pjfanning authored Nov 10, 2023
1 parent f13601d commit 1206d71
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/build-test-prValidation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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.
*/
Expand Down
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
8 changes: 8 additions & 0 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 1206d71

Please sign in to comment.