这部分总忘,记录下来,翻阅官方文档太麻烦了。
以2.4.3为例,想人工记录日志,可以
class MyLogger extends Actor{ val logger = Logging(context.system, this) def receive = { case "Go" => logger.debug("yes") println("ok") } }
也可以
class MyLogger2 extends Actor with ActorLogging{ def receive = { case "Go" => log.debug("yes") println("ok") } }
当然如果想看到debug的信息需要在application.conf中定义 akka.loglevel = DEBUG
想要将日志记录到其他地方比如磁盘文件中,需要引入
libraryDependencies += "com.typesafe.akka" % "akka-slf4j_2.11" % "2.4.3
libraryDependencies += "ch.qos.logback" % "logback-classic" % "1.1.7"
在application.conf中要设置akka.loggers = ["akka.event.slf4j.Slf4jLogger"], 默认是 ["akka.event.Logging$DefaultLogger"]
ch.qos.logback会读取classpath下的logback.xml文件,以一个即输出到控制台又写入到文件的配置为例
<?xml version="1.0" encoding="UTF-8"?> <configuration> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <pattern>[%level][%date] %msg%n</pattern> </encoder> </appender> <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <FileNamePattern>E://tmp/kumquatt-development.%d{yyyy-MM-dd}.log</FileNamePattern> </rollingPolicy> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <pattern>[%level][%date] %msg%n</pattern> </encoder> </appender> <root level="DEBUG"> <appender-ref ref="STDOUT" /> <appender-ref ref="FILE" /> </root> </configuration>