我已尝试以下步骤配置mykka系统的日志记录:
1.处理application.conf和logback.xml文件,并将它们放在src / main / resources目录中 2.application.conf文件如下所示: -
akka {
loggers = ["akka.event.slf4j.Slf4jLogger"]
logging-filter="akka.event.slf4j.Slf4jLoggingFilter"
log-config-on-start = on
loglevel = "DEBUG"
}
3.logback.xml文件如下所示: -
<?xml version="1.0" encoding="UTF-8"?>
<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>/Users/deepak/work/logs/akka.log</file>
<append>true</append>
<encoder>
<pattern>%date{yyyy-MM-dd} %X{akkaTimestamp} %-5level[%thread] %logger{1} - %msg%n</pattern>
</encoder>
</appender>
<logger name="akka" level="DEBUG" />
<root level="DEBUG">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
</root>
4.sbt构建依赖项: -
libraryDependencies += "com.typesafe.akka" % "akka-slf4j_2.11" % "2.4.14"
libraryDependencies += "ch.qos.logback" % "logback-classic" % "1.1.3" % Test
5.Akka Version =“2.4.14”
6.在构建jar文件后,我检查了application.conf,并在其中存在logback.xml文件。所以,我不认为这是任何类路径问题
7.我的演员混合了ActorLogging特质。但是当我运行jar文件时,我无法看到日志
import akka.actor.{Actor, ActorLogging}
class Reader extends Actor with ActorLogging{
override def receive = {
case _ =>log.info("Reader")
}
}
请帮助。我无法弄清楚问题
答案 0 :(得分:1)
快速浏览后,我看到了项目中的两个主要问题:
您的logback.xml
格式错误。它应以<configuration>
开头,以</configuration>
您的build.sbt
有问题。导入logback-classic
时,您应该从该行中删除Test
标记。否则,您只能在test
类中使用该库。
此外,我建议您使用最新版本的akka-slf4j_2.11
和logback-classic
。在这种情况下,这不会造成任何伤害,但坚持使用最新版本。
以下是项目的更正版本:
<强> build.sbt 强>
scalaVersion := "2.11.8"
libraryDependencies += "com.typesafe.akka" % "akka-slf4j_2.11" % "2.5.4"
//Removed the Test flag
libraryDependencies += "ch.qos.logback" % "logback-classic" % "1.1.7"
<强> logback.xml 强>
<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>akka.log</file>
<append>true</append>
<encoder>
<pattern>%date{yyyy-MM-dd} %X{akkaTimestamp} %-5level[%thread] %logger{1} - %msg%n</pattern>
</encoder>
</appender>
<logger name="akka" level="DEBUG"/>
<root level="DEBUG">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
</root>
</configuration>
<强> application.conf 强>
akka {
loggers = ["akka.event.slf4j.Slf4jLogger"]
logging-filter = "akka.event.slf4j.Slf4jLoggingFilter"
log-config-on-start = on
loglevel = "DEBUG"
}
<强> Reader.scala 强>
import akka.actor.{Actor, ActorLogging}
class Reader extends Actor with ActorLogging {
override def receive = {
case _ => log.info("Message received")
}
}
<强> Main.scala 强>
import akka.actor.{ActorRef, ActorSystem, Props}
object Main {
def main(args: Array[String]): Unit = {
implicit val system: ActorSystem = ActorSystem("reader-system")
val ref: ActorRef = system.actorOf(Props(new Reader))
//send message to actor
ref ! "message"
}
}
使用此配置,您应该能够运行Main.scala
,并在Message received
和std out
中查看Log file
日志。
希望这有帮助!