MaatDeamon
MaatDeamon

Reputation: 9761

Logging within Akka TestKit outside Actors

I have been trying to Log things within my scalaTest as such:

class ChangeSetActorTest extends PersistenceSpec(ActorSystem("Persistent-test-System")) with PersistenceCleanup {


  val log = Logging(system, this)

Basically let's just say that ChangesetActorTest inherit from TestKit(system)

Unfortunately Logging(system, this) does not work with the this.

I get the following error:

[error] /Users/maatary/Dev/IdeaProjects/PoolpartyConnector/src/test/scala/org/iadb/poolpartyconnector/changepropagation/ChangeSetActorTest.scala:22: Cannot find LogSource for org.iadb.poolpartyconnector.changepropagation.ChangeSetActorTest please see ScalaDoc for LogSource for how to obtain or construct one. [error] val log = Logging(system, this)

I believe in the Akka Logging Doc this is the following point: and in all other cases a compile error occurs unless and implicit LogSource[T] is in scope for the type in question.

In other words there is no LogSource[TestKit]

I would like the simplest solution to deal with that issue, with minimal additional configuration. So far what i did is the following and everything works as expected:

class ChangeSetActorTest extends PersistenceSpec(ActorSystem("Persistent-test-System")) with PersistenceCleanup {


      val log = system.log

From there I just go and do things like

val received = chgtFetcher.receiveWhile((requestInterval + ProcessingLag).*(3)) {
  case msg:FetchNewChangeSet => log.info(s"received: ${msg}" ) ; chgtFetcher.reply(NoAvailableChangeSet); msg
}

My question, is this recommended approach. So far the order of the message coming from my actor and the one from the test are well ordered.

What is the recommended approach to log in a unified way:

  1. From the Test class (e.g. above) and the Actor at the same time ?

  2. If one uses a system where external class needs to log as well and we need one unified logging (asynchronous) going on.

Upvotes: 3

Views: 627

Answers (2)

SwiftMango
SwiftMango

Reputation: 15284

Simplest way to log in a TestKit is either:

  1. Get the logger from underlyingActor:

    val mockActor = TestActorRef(new XXXActor)
    val log = mockActor.underlyingActor.log
    
  2. Use FeatureSpecLike

http://doc.scalatest.org/3.0.1-2.12/org/scalatest/FeatureSpecLike.html

class ChangeSetActorTest extends PersistenceSpec(ActorSystem("Persistent-test-System")) with PersistenceCleanup with FeatureSpecLike {

//...
alert("Something like warning")
info("Infos")
note("Green infos")
markup("documents")

}

Upvotes: 0

gilad hoch
gilad hoch

Reputation: 2866

Have a look at this comment: https://github.com/akka/akka/blob/master/akka-actor/src/main/scala/akka/event/Logging.scala#L196-L237

I believe a more straight forward approach would be to define your implicit LogSource[ChangeSetActorTest] locally.

I.E:

val log = {
  implicit val logSource = new LogSource[ChangeSetActorTest] {
    override def genString(t: ChangeSetActorTest) = "ChangeSetActorTest"
  }
  Logging(system, this)
}

Upvotes: 0

Related Questions