Akka SLF4J logback configuration and usage

前端 未结 4 1674
-上瘾入骨i
-上瘾入骨i 2020-12-08 01:08

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.

相关标签:
4条回答
  • I came across this problem too but in my case it wasn't a matter of classpath.

    I replaced akka.event.Logging with com.typesafe.scalalogging.slf4j.Logging and it works like a charm!

    my logback.xml (under main/resource):

    <configuration>
      <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>logs/filename.log</file>
        <encoder>
          <pattern>%date %level %msg%n</pattern>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
          <maxIndex>10</maxIndex>
          <FileNamePattern>logs/filename.log.%i.gz</FileNamePattern>
        </rollingPolicy>
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
          <maxFileSize>20MB</maxFileSize>
        </triggeringPolicy>
      </appender>
      <root level="info">
        <appender-ref ref="FILE" />
      </root>
    </configuration>
    

    my pom.xml:

    <dependency>
      <groupId>com.typesafe</groupId>
      <artifactId>scalalogging-slf4j_2.10</artifactId>
      <version>1.0.1</version>
    </dependency>
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId>
      <version>1.0.13</version>
    </dependency>
    

    and in the code:

    import com.typesafe.scalalogging.slf4j.Logging
    
    class LoggingService extends Actor with Logging {
      // use logger.info() etc.
    }
    

    (didn't use application.conf)

    0 讨论(0)
  • 2020-12-08 01:40

    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!")
    
    0 讨论(0)
  • 2020-12-08 01:48

    The only thing I would change in your setup is to add the weiglewilczek slf4j adapter.

    I've got the following setup working just fine:

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

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
        <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <file>log/app.log</file>
    
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <fileNamePattern>log/app.%d{dd-MM-yyyy}.log</fileNamePattern>
                <maxHistory>60</maxHistory>
            </rollingPolicy>
    
            <encoder>
                <pattern>%d [%thread] [%class] %5p - %m%n</pattern>
            </encoder>
        </appender>
    
        <root level="INFO">
            <appender-ref ref="FILE"/>
        </root>
    </configuration>
    

    trait test extends com.weiglewilczek.slf4s.Logging {
      def logIt() {
        logger.info("this logs fine")
      }
    }
    
    0 讨论(0)
  • 2020-12-08 01:52

    try using this application.conf:

    akka {
      loggers = ["akka.event.slf4j.Slf4jLogger"]
      loglevel = "DEBUG"
      logging-filter = "akka.event.slf4j.Slf4jLoggingFilter"
    }
    

    I also have noticed that you probably have wrong class name in you logback config for file appender. You have <appender name="FILE" class="ch.qos.logback.core. fileappender"> but it should be <appender name="FILE" class="ch.qos.logback.core.FileAppender">.

    Another issue may be the path to file. Try using absolute file path instead of relative. <file>/absolute/path/testFile.log</file>

    You can also try to change logback version to 1.2.3. Here are akka docs on configuring sl4j: link

    0 讨论(0)
提交回复
热议问题