隐式记录演员在Scala中收到的消息 [英] implicitly logging messages received by an actor in scala

查看:89
本文介绍了隐式记录演员在Scala中收到的消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对这种奇特的语言Scala是陌生的,我正在尝试创建一个用于记录Scala actor的库。我的图书馆有一个Logger类,它扩展了Actor类。
因此,任何使用actor模型的Scala应用程序都可以从此类扩展,也可以从Actor类扩展以添加日志功能。
例如MyClass类扩展了Logger。
在我的Logger类中,我实现了以下逻辑:每当有人将消息发送给某个actor时,该消息就会被发送actor的日志文件记录下来。
为此,每当有人向其他参与者发送消息而不是调用scala send函数时,例如

  caller! msg 

图书馆用户

  send(caller,msg)

然后库函数在日志中为此消息创建一个条目。这很好。
我希望为演员收到的消息添加类似的功能。也就是说,每当参与者收到消息时,库都应在日志文件中为接收到的消息创建一个条目。做到这一点的一种方法是,只要用户收到一个方法,他就可以显式调用这样的日志方法,

  ){
接收{
case msg =>
log(sender,msg)
//做一些事情
}
}

日志功能在日志中创建一个条目。
我希望用户不需要写此行日志(发送方,味精),以便该库对用户是透明的,并且没有用户需要编写的添加代码,并且所有内容都由隐式记录记录器类。
有没有办法做到这一点?
预先感谢。

解决方案

在Logger类中,重写方法 receive ,以便调用 log(sender,msg),然后调用 super.receive()。 / p>

I am new to this fancy language scala and I am trying to create a library for logging scala actors. My library has a class Logger which extends the Actor class. So any Scala application which uses actor model can extend from this class instead or the Actor class for added logging functionality. Eg. class MyClass extends Logger. In my Logger class I have implemented the logic that whenever someone sends a message to some actor it is logged by the log file of the sending actor. For this, whenever someone sends a message to another actor instead of calling the scala send function, like

caller ! msg

the user of the library calls

send(caller,msg)

The library function will then create an entry for this message in the log. This works fine. I wish to add similar functionality for messages received by an actor. That is, whenever an actor receives a message the library should make an entry for the received message in the log file. One way of doing this is that whenever the user receives a method he can explicitly call a log method like this,

while (true) {
   receive {
     case msg =>
       log(sender, msg)
       // do some stuff
   }
}

The log function makes an entry in the log. I want that the user should not need to write this line log(sender, msg) so that the library is transparent to the user and there is no added code which the user needs to write and everything is implicitly logged by the Logger class. Is there a way to achieve this? Thanks in advance.

解决方案

in the class Logger, override the method receive so that it calls log(sender, msg) and then super.receive().

这篇关于隐式记录演员在Scala中收到的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆