Search code examples
scalaloggingakkaslf4jactor

Akka logging pattern unchangeable


just building a small actor application for my bachelor thesis and now I am trying to add some logging.

For logging inside the actorsystem I want to use the logging akka provides with logback-classic in the background. The logging is working well so far but the pattern inside the actor system doesn't change when I change it in the logback.xml

Anyone an idea how to change the pattern that is global and used by logging via slf4j and logging inside the actorsystem?

log output outside the actor system:

2014-07-11 13:03:09 INFO  model.AccessLayer : New Object 10 with id:188586187441591506  `
2014-07-11 13:03:09 INFO  model.AccessLayer : New Object 11 with id:188586187442115794`

log output from inside the actor system:

[INFO] [07/11/2014 13:03:09.199] [EBTreeSimulation-akka.actor.default-dispatcher-4] [akka://EBTreeSimulation/user/nodeA] nodeA[InsertNewObject] received new object:188586187441591506, 188586187441591506, 10 

[INFO] [07/11/2014 13:03:09.199] [EBTreeSimulation-akka.actor.default-dispatcher-3] [akka://EBTreeSimulation/user/nodeB] nodeB[InsertNewObject] received new object:188586187441591506, 188586187441591506, 10

Logger initialisation for slf4j:

import org.slf4j.LoggerFactory

class AccessLayer[T](communicationLayer:ActorRef, actors:List[ActorRef]) {
  val log = LoggerFactory.getLogger(classOf[AccessLayer[T]])
  ....
}

Logger initialisation for actorSystem:

import akka.event.Logging
import com.typesafe.config.ConfigFactory

object SimulationMaster extends App{
   val system = ActorSystem("EBTreeSimulation", ConfigFactory.load.getConfig("akka"))
   val log = Logging.getLogger(system,this)
   ....
}

import akka.event.Logging

class TreeActor[T](communication:ActorRef) extends Actor {
   val log = Logging(context.system,this)
   ....

}

logback.xml:

<configuration>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <File>./logs/myLog.log</File>
    <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
        <fileNamePattern>logs/myLog.%i.log.zip</fileNamePattern>
        <minIndex>1</minIndex>
        <maxIndex>3</maxIndex>
    </rollingPolicy>
    <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
        <maxFileSize>5MB</maxFileSize>
    </triggeringPolicy>
    <encoder>
        <pattern>%date{YYYY-MM-dd HH:mm:ss} %level %X{sourceThread} %logger{10} [%file:%line]: %msg%n</pattern>
    </encoder>
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <!--<target>System.out</target>-->
    <encoder>
        <pattern>%date{YYYY-MM-dd HH:mm:ss} %-5level %logger : %msg%n</pattern>
    </encoder>
</appender>
<!--<logger name="akka" level="INFO" />-->
<root level="info">
    <appender-ref ref="FILE"/>
    <appender-ref ref="STDOUT" />
</root>

application.conf:

akka {
    # Loggers to register at boot time (akka.event.Logging$DefaultLogger logs
    # to STDOUT)
    loggers = ["akka.event.slf4j.Slf4jLogger"]
    #event-handlers = ["akka.event.slf4j.Slf4jEventHandler"]

    loglevel = "INFO"   
    stdout-loglevel = "DEBUG"
    actor {
       provider = "akka.cluster.ClusterActorRefProvider"
        default-dispatcher {
            throughput = 10
        }
    }
    remote {
        netty.tcp.port = 4711
    }
}

Solution

  • The problem was that akka load the default config instead of the customized one. To solve the Problem the loading of the configuration had to be changed:

    import akka.event.Logging import com.typesafe.config.ConfigFactory

    object SimulationMaster extends App{
       val system = ActorSystem("EBTreeSimulation", ConfigFactory.load)
       val log = Logging.getLogger(system,this)
       ....
    }
    

    Thanks to cmbaxter!