@xtccc
2016-09-06T16:25:27.000000Z
字数 3421
阅读 2159
Akka
参考链接
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.
dependencies {
compile "com.typesafe.akka:akka-slf4j_2.10:2.3.11",
"ch.qos.logback:logback-classic:1.1.3"
}
akka {
loggers = ["akka.event.slf4j.Slf4jLogger"]
loglevel = "INFO"
logging-filter = "akka.event.slf4j.Slf4jLoggingFilter"
}
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<target>System.out</target>
<encoder>
<pattern>%date{MM/dd HH:mm:ss} %-5level %logger{35} %L - %cyan(%msg%n)</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>log/akka.log</file>
<append>false</append>
<encoder>
<pattern>%date{MM/dd HH:mm:ss} %-5level %logger{35} %L - %cyan(%msg%n)</pattern>
</encoder>
</appender>
<logger name="akka" level="INFO" />
<root level="DEBUG">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
</root>
</configuration>
其中, %cyan(%msg%n)表示用不同的颜色来显示不同的内容(根据日志级别)
package cn.gridx.scala.spray.routing
import akka.actor.{Actor, ActorLogging}
import spray.can.Http
import spray.http.{HttpEntity, HttpRequest, HttpResponse}
import spray.http.HttpMethods.GET
import spray.http.MediaTypes._
/**
* Created by tao on 9/5/16.
*/
class HttpRequestService extends Actor with ActorLogging {
def actorRefFactory = context
override def receive = {
// when a new connection comes in we register ourselves as the connection handler
// 一定需要这一步
case _: Http.Connected =>
log.info("收到 `Http.Connected` ")
sender() ! Http.Register(self)
// 收到请求后构造一个HttpResponse
case HttpRequest(GET, path, headers, entity, protocol) =>
val msg = s"收到GET请求, \n\theaders = ${headers}, entity = ${entity}, protocol = ${protocol}"
log.info(msg)
sender() ! GenHttpResp(msg)
}
def GenHttpResp(msg: String) = HttpResponse(
entity = HttpEntity(`text/html`,
<html>
<body>
<h1>Header 1</h1>
<h2>$msg</h2>
</body>
</html>
.toString()
)
)
}
运行结果:
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
由于日志的异步性,输出的日志中:
参考