Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
804 views
in Technique[技术] by (71.8m points)

scala - Akka SLF4J logback configuration and usage

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:

http://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!")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...