Akka doc的“The Actor DSL”示例中是否有可用的记录器?

时间:2013-12-06 22:16:33

标签: scala akka

我对Scala / Akka堆栈完全不熟悉,所以如果这个问题听起来很原始,请原谅。我正在使用文档(http://doc.akka.io/docs/akka/snapshot/scala/actors.html#The_Actor_DSL)中的“The Actor DSL”示例,并且正在努力弄清楚如何在那里获取记录器以便于我理解事件的顺序。本章前面的例子展示了如何使用...

在Actor的定义中获取记录器
import akka.event.Logging
...
val log = Logging(context.system, this)

但是当我在REPL中尝试这个时,我得到的是:“15:错误:递归值日志需要类型”。这几乎就是我的REPL的状态。问题出在底部......

bobk-mbp:akka_hw_scala bobk$ sbt
> set scalacOptions in (Compile, console) += "-Yrepl-sync"
> console
scala> import akka.actor.ActorDSL._
import akka.actor.ActorDSL._

scala> import akka.actor.ActorSystem
ActorSystem       ActorSystemImpl   

scala> import akka.actor.ActorSystem
import akka.actor.ActorSystem

scala> implicit val system = ActorSystem("demo")
system: akka.actor.ActorSystem = akka://demo

scala> import akka.event.Logging
import akka.event.Logging

scala> :paste
// Entering paste mode (ctrl-D to finish)

val a = actor(
  new Act {
    val log = Logging( context.system, this )
    become {
      case "info" => log.info( "A" )
      case "switch" => becomeStacked {
        case "info" => log.info( "B" )
        case "switch" => unbecome()
      }
      case "lobotomize" => unbecome()
    }
  }
)

// Exiting paste mode, now interpreting.

<console>:15: error: recursive value log needs type
           val log = Logging( context.system, this )
               ^

scala>

如何在REPL中为ActorDSL定义Actors提供记录器?

2 个答案:

答案 0 :(得分:1)

使用Act with ActorLogginglog课程中获得Act

val a = actor(
  new Act with ActorLogging {
    become {
      case "info" => log.info( "A" )
      case "switch" => becomeStacked {
        case "info" => log.info( "B" )
        case "switch" => unbecome()
      }
      case "lobotomize" => unbecome()
    }
  }
)

答案 1 :(得分:0)

有趣的是,如果我不尝试将记录器绑定到我的val日志,它就可以工作......

val a = actor(
  new Act {
    become {
      case "info" => Logging( context.system, this ).info( "A" )
      case "switch" => becomeStacked {
        case "info" => Logging( context.system, this ).info( "B" )
        case "switch" => unbecome()
      }
      case "lobotomize" => unbecome()
    }
  }
)

...嗯。