mirelon
mirelon

Reputation: 4996

Log all messages in Akka without modifying all receive methods

I want to log all received messages to all actors in my Akka app. There is a config akka.actor.debug.receive that will log all messages sent to an actor if that actors receive method is a LoggingReceive.

According to http://doc.akka.io/docs/akka/current/additional/faq.html it means wrapping all receive methods with LoggingReceive as in How to log all incoming messages from Akka (Java)

def receive = {
  LoggingReceive {
    case x ⇒ // do something
  }
}

Is there a way to do this implicitly, or by config?

Upvotes: 7

Views: 1323

Answers (1)

cmbaxter
cmbaxter

Reputation: 35463

Not that I know of, but you should very easily be able to do something like this:

trait LoggingReceiveActor extends Actor{

  def receive = LoggingReceive(loggedReceive)

  def loggedReceive:Receive
}

class MyActor extends LoggingReceiveActor{

  def loggedReceive = {
    case _ => 
  }
}

Any actor that inherits from LoggingReceiveActor now has to provide an impl for loggingReceive and if you do that, then when debug logging is enabled then this kind of actor will log the messages received.

Upvotes: 6

Related Questions