Akka类型化日志记录示例

w41d8nur  于 2022-11-06  发布在  其他
关注(0)|答案(1)|浏览(134)

有人能分享一个最小的akka类型的日志示例吗?在akka classic中,我使用了LoggingReceive,并在src/main/resources/application.conf中设置了

akka {
  loglevel = DEBUG
  actor {
    debug {
      receive=on
    }
  }
}

但是在Akka中我不清楚该怎么做。如果可能的话,我希望日志记录也能与akka.actor.testkit一起工作
我尝试按照建议简单地添加here

Behaviors.receive[String] { (ctx, message) =>
  ctx.log.info("Received message: {}", message)
  Behaviors.same
}

但是没有打印任何东西,这让我觉得我可能需要以某种方式配置后端,也许是用logback.xml(尽管我不明白这一点)

30byixjq

30byixjq1#

您可以使用ConfigFactory.load().getConfig()并将特定的配置加载到应用程序中。
就像这是我的application.conf

akka.actor.allow-java-serialization = on
mySpecialConfig {
    akka {
        loglevel = DEBUG
    }
}

和我的应用程序,在其中我可以直接调用context.log.debug(),因为AbstractBehavior已经扩展了ActorLogging。

import akka.actor.typed._
import akka.actor.typed.scaladsl.{AbstractBehavior, ActorContext, Behaviors}
import com.typesafe.config.ConfigFactory

object CounterActorTypedDemo {
  def main(args: Array[String]): Unit = {
    run()
  }

  def run(): Unit = {

    import Counter._

    val specialConfig = ConfigFactory.load().getConfig("mySpecialConfig")
    val countActor = ActorSystem(CounterActorTyped(), "CounterSystem", specialConfig)
    (1 to 5).foreach(_ => countActor ! Increment)
    (1 to 3).foreach(_ => countActor ! Decrement)
    countActor ! Print

    Thread.sleep(5000)
    countActor.terminate
  }
}

object Counter {

  trait CounterMsg

  final case object Print extends CounterMsg

  final case object Increment extends CounterMsg

  final case object Decrement extends CounterMsg

}

object CounterActorTyped {
  def apply(): Behavior[Counter.CounterMsg] = Behaviors.setup[Counter.CounterMsg](context => new CounterActorTyped(context))
}

class CounterActorTyped(context: ActorContext[Counter.CounterMsg]) extends AbstractBehavior[Counter.CounterMsg](context) {
  context.log.info("Counter Application started")
  var count = 0

  import Counter._

  override def onMessage(msg: CounterMsg): Behavior[CounterMsg] = msg match {
    case Increment =>
      context.log.debug(s"incrementing $count ...")
      count += 1
      Behaviors.same
    case Decrement =>
      context.log.debug(s"decrementing $count ...")
      count -= 1
      Behaviors.same
    case Print =>
      context.log.debug(s"current count is: $count")
      Behaviors.same
  }

  override def onSignal: PartialFunction[Signal, Behavior[CounterMsg]] = {
    case PostStop =>
      context.log.info("Counter Application stopped")
      this
  }
}

日志将在控制台上显示如下:

org.github.felipegutierrez.explore.akka.typed.basics.CounterActorTypedDemo
    10:00:35.944 [CounterSystem-akka.actor.default-dispatcher-3] INFO akka.event.slf4j.Slf4jLogger - Slf4jLogger started
    10:00:35.946 [CounterSystem-akka.actor.default-dispatcher-3] DEBUG akka.event.EventStream - logger log1-Slf4jLogger started
    10:00:35.951 [CounterSystem-akka.actor.default-dispatcher-3] DEBUG akka.event.EventStream - Default Loggers started
    SLF4J: A number (3) of logging calls during the initialization phase have been intercepted and are
    SLF4J: now being replayed. These are subject to the filtering rules of the underlying logging system.
    SLF4J: See also http://www.slf4j.org/codes.html#replay
    10:00:36.219 [CounterSystem-akka.actor.default-dispatcher-3] DEBUG akka.serialization.Serialization(akka://CounterSystem) - Replacing JavaSerializer with DisabledJavaSerializer, due to `akka.actor.allow-java-serialization = off`.
    10:00:36.762 [CounterSystem-akka.actor.default-dispatcher-3] INFO org.github.felipegutierrez.explore.akka.typed.basics.CounterActorTyped - Counter Application started
    10:00:36.763 [CounterSystem-akka.actor.default-dispatcher-3] DEBUG org.github.felipegutierrez.explore.akka.typed.basics.CounterActorTyped - incrementing 0 ...
    10:00:36.763 [CounterSystem-akka.actor.default-dispatcher-3] DEBUG org.github.felipegutierrez.explore.akka.typed.basics.CounterActorTyped - incrementing 1 ...
    10:00:36.763 [CounterSystem-akka.actor.default-dispatcher-3] DEBUG org.github.felipegutierrez.explore.akka.typed.basics.CounterActorTyped - incrementing 2 ...
    10:00:36.764 [CounterSystem-akka.actor.default-dispatcher-3] DEBUG org.github.felipegutierrez.explore.akka.typed.basics.CounterActorTyped - incrementing 3 ...
    10:00:36.764 [CounterSystem-akka.actor.default-dispatcher-3] DEBUG org.github.felipegutierrez.explore.akka.typed.basics.CounterActorTyped - incrementing 4 ...
    10:00:36.764 [CounterSystem-akka.actor.default-dispatcher-3] DEBUG org.github.felipegutierrez.explore.akka.typed.basics.CounterActorTyped - decrementing 5 ...
    10:00:36.765 [CounterSystem-akka.actor.default-dispatcher-3] DEBUG org.github.felipegutierrez.explore.akka.typed.basics.CounterActorTyped - decrementing 4 ...
    10:00:36.765 [CounterSystem-akka.actor.default-dispatcher-3] DEBUG org.github.felipegutierrez.explore.akka.typed.basics.CounterActorTyped - decrementing 3 ...
    10:00:36.765 [CounterSystem-akka.actor.default-dispatcher-3] DEBUG org.github.felipegutierrez.explore.akka.typed.basics.CounterActorTyped - current count is: 2
    10:00:41.780 [CounterSystem-akka.actor.default-dispatcher-3] DEBUG akka.actor.CoordinatedShutdown - Running CoordinatedShutdown with reason [ActorSystemTerminateReason]
    10:00:41.780 [CounterSystem-akka.actor.default-dispatcher-3] DEBUG akka.actor.CoordinatedShutdown - Performing phase [before-service-unbind] with [0] tasks
    10:00:41.788 [CounterSystem-akka.actor.default-dispatcher-3] DEBUG akka.actor.CoordinatedShutdown - Performing phase [service-unbind] with [0] tasks
    10:00:41.789 [CounterSystem-akka.actor.default-dispatcher-3] DEBUG akka.actor.CoordinatedShutdown - Performing phase [service-requests-done] with [0] tasks
    10:00:41.789 [CounterSystem-akka.actor.default-dispatcher-3] DEBUG akka.actor.CoordinatedShutdown - Performing phase [service-stop] with [0] tasks
    10:00:41.789 [CounterSystem-akka.actor.default-dispatcher-3] DEBUG akka.actor.CoordinatedShutdown - Performing phase [before-cluster-shutdown] with [0] tasks
    10:00:41.789 [CounterSystem-akka.actor.default-dispatcher-3] DEBUG akka.actor.CoordinatedShutdown - Performing phase [cluster-sharding-shutdown-region] with [0] tasks
    10:00:41.790 [CounterSystem-akka.actor.default-dispatcher-3] DEBUG akka.actor.CoordinatedShutdown - Performing phase [cluster-leave] with [0] tasks
    10:00:41.790 [CounterSystem-akka.actor.default-dispatcher-3] DEBUG akka.actor.CoordinatedShutdown - Performing phase [cluster-exiting] with [0] tasks
    10:00:41.790 [CounterSystem-akka.actor.default-dispatcher-3] DEBUG akka.actor.CoordinatedShutdown - Performing phase [cluster-exiting-done] with [0] tasks
    10:00:41.790 [CounterSystem-akka.actor.default-dispatcher-3] DEBUG akka.actor.CoordinatedShutdown - Performing phase [cluster-shutdown] with [0] tasks
    10:00:41.790 [CounterSystem-akka.actor.default-dispatcher-3] DEBUG akka.actor.CoordinatedShutdown - Performing phase [before-actor-system-terminate] with [0] tasks
    10:00:41.791 [CounterSystem-akka.actor.default-dispatcher-3] DEBUG akka.actor.CoordinatedShutdown - Performing phase [actor-system-terminate] with [1] tasks.
    10:00:41.793 [CounterSystem-akka.actor.default-dispatcher-3] DEBUG akka.actor.CoordinatedShutdown - Performing task [terminate-system] in CoordinatedShutdown phase [actor-system-terminate]
    10:00:41.800 [CounterSystem-akka.actor.default-dispatcher-3] INFO org.github.felipegutierrez.explore.akka.typed.basics.CounterActorTyped - Counter Application stopped
    10:00:41.819 [CounterSystem-akka.actor.default-dispatcher-3] DEBUG akka.event.EventStream - shutting down: StandardOutLogger

    Process finished with exit code 0

相关问题