diff --git a/akka-actor/src/main/scala/akka/event/LoggingReceive.scala b/akka-actor/src/main/scala/akka/event/LoggingReceive.scala index 8388ab5c9c..ac3e9d6fe1 100644 --- a/akka-actor/src/main/scala/akka/event/LoggingReceive.scala +++ b/akka-actor/src/main/scala/akka/event/LoggingReceive.scala @@ -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) diff --git a/akka-docs/rst/scala/code/docs/testkit/TestkitDocSpec.scala b/akka-docs/rst/scala/code/docs/testkit/TestkitDocSpec.scala index 78d965b710..14d3070120 100644 --- a/akka-docs/rst/scala/code/docs/testkit/TestkitDocSpec.scala +++ b/akka-docs/rst/scala/code/docs/testkit/TestkitDocSpec.scala @@ -74,7 +74,10 @@ object TestkitDocSpec { //#logging-receive import akka.event.LoggingReceive def receive = LoggingReceive { - case msg => // Do something... + case msg => // Do something ... + } + def otherState: Receive = LoggingReceive.withLabel("other") { + case msg => // Do something else ... } //#logging-receive }