[关闭]
@xtccc 2016-09-06T16:25:27.000000Z 字数 3421 阅读 2136

Logging

Akka



参考链接



Akka的logging机制


Internally, when we log a message, the the logging methods in the ActorLogging (eventually) publishes the log message to an EventStream.

EventStream behaves just like a message broker to which we could publish and receive messages. One subtle distinction from a regular MOM is that the subscribers of the EventStream could only be an Actor.

In case of logging messages, all log messages would be published to the EventStream. By default, the Actor that subscribes to these messages is the DefaultLogger which simply prints the message to the standard output.


添加依赖


  1. dependencies {
  2. compile "com.typesafe.akka:akka-slf4j_2.10:2.3.11",
  3. "ch.qos.logback:logback-classic:1.1.3"
  4. }




resources/application.conf


  1. akka {
  2. loggers = ["akka.event.slf4j.Slf4jLogger"]
  3. loglevel = "INFO"
  4. logging-filter = "akka.event.slf4j.Slf4jLoggingFilter"
  5. }




resources/logback.xml


  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <configuration>
  3. <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
  4. <target>System.out</target>
  5. <encoder>
  6. <pattern>%date{MM/dd HH:mm:ss} %-5level %logger{35} %L - %cyan(%msg%n)</pattern>
  7. </encoder>
  8. </appender>
  9. <appender name="FILE" class="ch.qos.logback.core.FileAppender">
  10. <file>log/akka.log</file>
  11. <append>false</append>
  12. <encoder>
  13. <pattern>%date{MM/dd HH:mm:ss} %-5level %logger{35} %L - %cyan(%msg%n)</pattern>
  14. </encoder>
  15. </appender>
  16. <logger name="akka" level="INFO" />
  17. <root level="DEBUG">
  18. <appender-ref ref="CONSOLE"/>
  19. <appender-ref ref="FILE"/>
  20. </root>
  21. </configuration>

其中, %cyan(%msg%n)表示用不同的颜色来显示不同的内容(根据日志级别)




调用API


  1. package cn.gridx.scala.spray.routing
  2. import akka.actor.{Actor, ActorLogging}
  3. import spray.can.Http
  4. import spray.http.{HttpEntity, HttpRequest, HttpResponse}
  5. import spray.http.HttpMethods.GET
  6. import spray.http.MediaTypes._
  7. /**
  8. * Created by tao on 9/5/16.
  9. */
  10. class HttpRequestService extends Actor with ActorLogging {
  11. def actorRefFactory = context
  12. override def receive = {
  13. // when a new connection comes in we register ourselves as the connection handler
  14. // 一定需要这一步
  15. case _: Http.Connected =>
  16. log.info("收到 `Http.Connected` ")
  17. sender() ! Http.Register(self)
  18. // 收到请求后构造一个HttpResponse
  19. case HttpRequest(GET, path, headers, entity, protocol) =>
  20. val msg = s"收到GET请求, \n\theaders = ${headers}, entity = ${entity}, protocol = ${protocol}"
  21. log.info(msg)
  22. sender() ! GenHttpResp(msg)
  23. }
  24. def GenHttpResp(msg: String) = HttpResponse(
  25. entity = HttpEntity(`text/html`,
  26. <html>
  27. <body>
  28. <h1>Header 1</h1>
  29. <h2>$msg</h2>
  30. </body>
  31. </html>
  32. .toString()
  33. )
  34. )
  35. }



运行结果:

2016/09/05 03:09:46 INFO akka.event.slf4j.Slf4jLogger - Slf4jLogger started
2016/09/05 03:09:47 INFO spray.can.server.HttpListener - Bound to localhost/127.0.0.1:8881
2016/09/05 03:09:47 INFO spray.can.server.HttpListener - Bound to localhost/127.0.0.1:8882
2016/09/05 03:09:47 INFO spray.can.server.HttpListener - Bound to localhost/127.0.0.1:8883
2016/09/05 03:09:51 INFO cn.gridx.scala.spray.routing.HttpRequestService - 收到 Http.Connected
2016/09/05 03:09:51 INFO cn.gridx.scala.spray.routing.HttpRequestService - 收到 Http.Connected
2016/09/05 03:09:52 INFO cn.gridx.scala.spray.routing.HttpRequestService - 收到GET请求,
headers = List(Host: localhost:8883, Connection: keep-alive, Cache-Control: max-age=0, Upgrade-Insecure-Requests: 1, User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36, Accept: text/html, application/xhtml+xml, application/xml;q=0.9, image/webp, /;q=0.8, Accept-Encoding: gzip, deflate, sdch, Accept-Language: en-US, en;q=0.8, zh-CN;q=0.6, zh;q=0.4), entity = Empty, protocol = HTTP/1.1




局限性


由于日志的异步性,输出的日志中:

  1. 时间不是日志被触发的真正时间;
  2. 行号不是真正的代码行号



参考

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注