akka slf4j不起作用



我尝试了以下步骤为我的akka系统配置记录:

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版本=" 2.4.14"
6.构建JAR文件后,我检查了application.conf和logback.xml文件。因此,我认为这不是任何集体路径问题。
7.我的演员与演员特征混合。但是当我运行jar文件时,我将无法看到日志

    import akka.actor.{Actor, ActorLogging}  
    class Reader extends Actor with ActorLogging{  
     override def receive = {  
      case _ =>log.info("Reader")  
     }  
    }  

请在这里帮忙。我无法弄清楚这个问题

快速查看后,我看到了您项目中的两个主要问题:

  1. 您的logback.xml格式错误。它应该从<configuration>开始,并以</configuration>

  2. 结尾
  3. 您的build.sbt有问题。导入logback-classic时,您应该从该行删除Test标志。否则,您只能在test类中使用该库。

  4. 此外,我建议您使用akka-slf4j_2.11logback-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并在std outLog file中查看Message received日志。

希望这会有所帮助!

相关内容

  • 没有找到相关文章

最新更新