Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to log all incoming messages from Akka (Java)

In Scala you can wrap the receive function with a LoggingReceive. How do you achieve the same from the Java API?

def receive = {
  LoggingReceive {
    case x ⇒ // do something
  }
}
like image 816
Chris Avatar asked Mar 21 '23 01:03

Chris


1 Answers

The Scala API has the LoggingReceive decorator because a partial function literal makes it awkward to express something to be done in all cases (like this logging).

In Java you don’t have this problem because your onReceive method is always called, and you can put a logging statement at the top to see all messages which are received by the actor. As an added bonus you get to decide at which level to log them ;-)

If you want to make your logging conditional on the config setting akka.actor.debug.receive (just as for Scala), then you can say for example

if (getContext().system().settings().AddLoggingReceive())
  log.debug("received message of type {}", msg.getClass());
like image 111
Roland Kuhn Avatar answered Mar 31 '23 21:03

Roland Kuhn