2

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
    }
}
2
  • I think your problem might have to do with this line ConfigFactory.load.getConfig("akka"). Try and change it to just ConfigFactory.load. That way it sees the child 'akka' element and gets the modified logging config from it.
    – cmbaxter
    Commented Jul 11, 2014 at 12:38
  • Great solved the problem! Thanks a lot
    – prototyp
    Commented Jul 11, 2014 at 21:19

2 Answers 2

2

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!

0

The other answers have effectively answered this question. However, I had several other related issues.

To help debug the problem, you can log the ActorSystem configuration as follows:

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

In this output I saw that the akka.loggers was still set to some default logger instead of the slf4j logger. Looking further into the configuration, I found that the slf4j logger was set, but it was set to the singular akka.logger. I pluralized it and then everything started working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.