+act #3949 add LoggingReceive.withLabel
def someState = LoggingReceive.withLabel("some state") {
case msg => ...
}
This commit is contained in:
parent
a9c022e92a
commit
dc305eaf18
2 changed files with 20 additions and 7 deletions
|
|
@ -26,27 +26,37 @@ object LoggingReceive {
|
|||
* This method does NOT modify the given Receive unless
|
||||
* `akka.actor.debug.receive` is set in configuration.
|
||||
*/
|
||||
def apply(r: Receive)(implicit context: ActorContext): Receive = r match {
|
||||
case _: LoggingReceive ⇒ r
|
||||
case _ ⇒ if (context.system.settings.AddLoggingReceive) new LoggingReceive(None, r) else r
|
||||
}
|
||||
def apply(r: Receive)(implicit context: ActorContext): Receive = withLabel(null)(r)
|
||||
|
||||
/**
|
||||
* Java API: compatible with lambda expressions
|
||||
* This is an EXPERIMENTAL feature and is subject to change until it has received more real world testing.
|
||||
*/
|
||||
def create(r: Receive, context: ActorContext): Receive = apply(r)(context)
|
||||
|
||||
/**
|
||||
* Create a decorated logger which will append `" in state " + label` to each message it logs.
|
||||
*/
|
||||
def withLabel(label: String)(r: Receive)(implicit context: ActorContext): Receive = r match {
|
||||
case _: LoggingReceive ⇒ r
|
||||
case _ ⇒ if (context.system.settings.AddLoggingReceive) new LoggingReceive(None, r, Option(label)) else r
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This decorator adds invocation logging to a Receive function.
|
||||
* @param source the log source, if not defined the actor of the context will be used
|
||||
*/
|
||||
class LoggingReceive(source: Option[AnyRef], r: Receive)(implicit context: ActorContext) extends Receive {
|
||||
class LoggingReceive(source: Option[AnyRef], r: Receive, label: Option[String])(implicit context: ActorContext) extends Receive {
|
||||
def this(source: Option[AnyRef], r: Receive)(implicit context: ActorContext) = this(source, r, None)
|
||||
def isDefinedAt(o: Any): Boolean = {
|
||||
val handled = r.isDefinedAt(o)
|
||||
val (str, clazz) = LogSource.fromAnyRef(source getOrElse context.asInstanceOf[ActorCell].actor)
|
||||
context.system.eventStream.publish(Debug(str, clazz, "received " + (if (handled) "handled" else "unhandled") + " message " + o))
|
||||
context.system.eventStream.publish(Debug(str, clazz, "received " + (if (handled) "handled" else "unhandled") + " message " + o
|
||||
+ (label match {
|
||||
case Some(l) ⇒ " in state " + l
|
||||
case _ ⇒ ""
|
||||
})))
|
||||
handled
|
||||
}
|
||||
def apply(o: Any): Unit = r(o)
|
||||
|
|
|
|||
|
|
@ -76,6 +76,9 @@ object TestkitDocSpec {
|
|||
def receive = LoggingReceive {
|
||||
case msg => // Do something ...
|
||||
}
|
||||
def otherState: Receive = LoggingReceive.withLabel("other") {
|
||||
case msg => // Do something else ...
|
||||
}
|
||||
//#logging-receive
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue