Logging all messages sent to an Akka TestKit TestProbe

akka, scala, testkit

Solution

Sorry, got your question a little wrong at first, so here is my approach.

Create a wrapper actor, that logs the messages:

class LoggingActor(fac: => Actor) extends Actor {

  val underlying = context.system.actorOf(Props(fac))

  def receive = {
    LoggingReceive {
      case x ⇒ underlying.tell(x, sender)
    }
  }
}

and then just create your `TestActorRef` with your actor wrapped in the `LoggingActor`:

  val echo = TestActorRef(new LoggingActor(new FooActor))

  echo ! "hello world"

Problem

I'm trying to log all messages received by a TestKit TestProbe, which is proving to be somewhat difficult. I'm aware of the Actor Logging section in the docs where it says one should use the `debug.receive` option in combination with a `LogginReceive` block. This however doesn't work when I'm not in control of the actor's implementation. The only idea I had was subclassing `akka.testkit.TestActor` to use a `LoggingReceive` and then subclass `TestKit` to make it create instances of my `TestActor` subclass instead, but that didn't work because most functionality there is private to the `akka` namespace (and for good reason, I suppose).

Original source