在Scala中,您可以使用LoggingReceive包装接收函数。您如何通过Java API实现相同的目的?
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 } }推荐答案
Scala API具有 LoggingReceive 装饰器,因为部分函数字面量使其难以表达在所有情况下(例如此日志记录)要做的事情。
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).
在Java中,您不会遇到此问题,因为您的 onReceive 方法总是被调用,并且您可以放置一条日志记录语句在顶部可以查看参与者收到的所有消息。作为额外的奖励,您可以决定将日志记录在哪个级别上;-)
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 ;-)
如果您希望以配置设置为条件进行记录akka.actor.debug.receive (与Scala一样),例如,您可以说
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());更多推荐
如何记录来自Akka的所有传入消息(Java)
发布评论