为我的学士论文建立一个小型演员应用程序,现在我正在尝试添加一些日志记录。
为了在actorsystem内部进行日志记录,我想在后台使用log akka提供的logback-classic。 到目前为止,日志记录运行良好,但是当我在logback.xml中更改它时,actor系统内的模式不会改变
任何人都知道如何更改全局模式并通过slf4j进行日志记录并在actorsystem中记录?
在演员系统外部记录输出:
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`
来自actor系统内部的日志输出:
[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
slf4j的记录器初始化:
import org.slf4j.LoggerFactory
class AccessLayer[T](communicationLayer:ActorRef, actors:List[ActorRef]) {
val log = LoggerFactory.getLogger(classOf[AccessLayer[T]])
....
}
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
}
}
答案 0 :(得分:2)
问题是akka加载默认配置而不是自定义配置。要解决问题,必须更改配置的加载:
导入akka.event.Logging import com.typesafe.config.ConfigFactory
object SimulationMaster extends App{
val system = ActorSystem("EBTreeSimulation", ConfigFactory.load)
val log = Logging.getLogger(system,this)
....
}
感谢cmbaxter!
答案 1 :(得分:0)
其他答案已经有效地回答了这个问题。但是,我还有其他一些相关问题。
为了帮助调试问题,您可以按如下方式记录ActorSystem配置:
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
....
}
在此输出中,我看到akka.loggers
仍设置为某个默认记录器而不是slf4j记录器。进一步查看配置,我发现slf4j记录器已设置,但设置为单数 akka.logger
。我把它复数,然后一切都开始工作了。