Akka slf4j不工作

时间:2017-09-21 06:40:08

标签: logging akka slf4j logback-classic

我已尝试以下步骤配置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")  
     }  
    }  

请帮助。我无法弄清楚问题

1 个答案:

答案 0 :(得分:1)

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

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

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

  4. 此外,我建议您使用最新版本的akka-slf4j_2.11logback-classic。在这种情况下,这不会造成任何伤害,但坚持使用最新版本。

  5. 以下是项目的更正版本:

    <强> 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 receivedstd out中查看Log file日志。

    希望这有帮助!