Akka SLF4J logback 配置和使用

编程入门 行业动态 更新时间:2024-10-25 15:36:22
本文介绍了Akka SLF4J logback 配置和使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已完成以下步骤来尝试为我的 akka 应用程序配置日志记录:

  • 创建了一个 application.conf 文件并将其放置在 src/main/resources 中.看起来像:

    阿卡{事件处理程序 = ["akka.event.slf4j.Slf4jEventHandler"]日志级别 = "信息"}

  • 创建了一个 logback.xml 文件并将其放置在 src/main/resources 中.看起来像:

    <appender name="FILE" class="ch.qos.logback.core.fileappender"><文件>./logs/akka.log</File><编码器><pattern>%d{HH:mm:ss.SSS} [%-5level] %msg%n</pattern></编码器></appender><root level="信息"><appender-ref ref="文件"/></root></配置>

  • 将以下内容添加到我的 .scala sbt 构建文件中:

    libraryDependencies += "com.typesafe.akka" % "akka-slf4j" % "2.0.3",libraryDependencies += "ch.qos.logback" % "logback-classic" % "1.0.9"懒惰的 val logback = "ch.qos.logback" % "logback-classic" % "1.0.9"

  • 尝试记录此代码:

    导入 akka.event.Loggingval log = Logging(context.system, this)log.info("...")

我得到的只是标准输出日志记录,没有使用日志创建日志文件.

我错过了一步吗?还是配置错误?

解决方案

这样安排我就可以使用 akka.event.Logging,无需指定 SLF4J 实例.

(2013 年 12 月 13 日测试)

我得到控制台日志记录并记录到文件.为了证明这不是内置记录器,我更改为包含 %X{akkaTimestamp},如下所述:

doc.akka.io/docs/akka/snapshot/scala/logging.html

build.sbt

库依赖:(Akka 2.2.3 版)..."com.typesafe.akka" %% "akka-slf4j" % "2.2.3"ch.qos.logback"%logback-经典"%1.0.9"...

src/main/resources/application.conf

akka {记录器 = ["akka.event.slf4j.Slf4jLogger"]日志级别 = "信息"}

src/main/resources/logback.xml

<?xml version="1.0" encoding="UTF-8"?><配置><appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"><target>System.out</target><编码器><pattern>%X{akkaTimestamp} %-5level[%thread] %logger{0} - %msg%n</pattern></编码器></appender><appender name="FILE" class="ch.qos.logback.core.FileAppender"><file>log/akka.log</file><append>false</append><编码器><pattern>%date{yyyy-MM-dd} %X{akkaTimestamp} %-5level[%thread] %logger{1} - %msg%n</pattern></编码器></appender><logger name="akka" level="INFO"/><root level="调试"><appender-ref ref="控制台"/><appender-ref ref="文件"/></root></配置>

当我使用 ActorLogging mixin 并直接创建 Logging 时,这种安排有效:

import akka.event.Loggingval log = Logging(context.system, classOf[NameOfYourActor])log.info("祝你好运!")

I have done the following steps to try and configure logging for my akka application:

  • created an application.conf file and placed it in src/main/resources. It looks like:

    akka { event-handlers = ["akka.event.slf4j.Slf4jEventHandler"] loglevel = "INFO" }

  • created a logback.xml file and placed it in src/main/resources. It looks like:

    <configuration> <appender name="FILE" class="ch.qos.logback.core.fileappender"> <File>./logs/akka.log</File> <encoder> <pattern>%d{HH:mm:ss.SSS} [%-5level] %msg%n</pattern> </encoder> </appender> <root level="info"> <appender-ref ref="FILE" /> </root> </configuration>

  • added the following to my .scala sbt build file:

    libraryDependencies += "com.typesafe.akka" % "akka-slf4j" % "2.0.3", libraryDependencies += "ch.qos.logback" % "logback-classic" % "1.0.9" lazy val logback = "ch.qos.logback" % "logback-classic" % "1.0.9"

  • attempted this code to log:

    import akka.event.Logging val log = Logging(context.system, this) log.info("...")

All I am getting is standard output logging, no log file creation with the logs.

Have I missed a step ? Or misconfigured something?

解决方案

With this arrangement I can use an akka.event.Logging, no need to specify SLF4J instance.

(tested 13 Dec 2013)

I get console logging and logging to a file. To prove this is not built-in logger I changed to include %X{akkaTimestamp} as explained here:

doc.akka.io/docs/akka/snapshot/scala/logging.html

build.sbt

library dependencies: (Akka version 2.2.3) ... "com.typesafe.akka" %% "akka-slf4j" % "2.2.3" "ch.qos.logback" % "logback-classic" % "1.0.9" ...

src/main/resources/application.conf

akka { loggers = ["akka.event.slf4j.Slf4jLogger"] loglevel = "INFO" }

src/main/resources/logback.xml

<?xml version="1.0" encoding="UTF-8"?> <configuration> <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> <target>System.out</target> <encoder> <pattern>%X{akkaTimestamp} %-5level[%thread] %logger{0} - %msg%n</pattern> </encoder> </appender> <appender name="FILE" class="ch.qos.logback.core.FileAppender"> <file>log/akka.log</file> <append>false</append> <encoder> <pattern>%date{yyyy-MM-dd} %X{akkaTimestamp} %-5level[%thread] %logger{1} - %msg%n</pattern> </encoder> </appender> <logger name="akka" level="INFO" /> <root level="DEBUG"> <appender-ref ref="CONSOLE"/> <appender-ref ref="FILE"/> </root> </configuration>

This arrangement works when I use an ActorLogging mixin and also create a Logging directly:

import akka.event.Logging val log = Logging(context.system, classOf[NameOfYourActor]) log.info("good luck!")

更多推荐

Akka SLF4J logback 配置和使用

本文发布于:2023-11-25 03:58:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1628225.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Akka   SLF4J   logback

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!