2

I'm using unfiltered to provide restful API, and have defined several intents. Now I have a new requirement, I have to log all the request url into a file, but I can't find a good solution for it.

I've read the document of unfiltered, not found something like "filter/interceptor" in SpringMVC. Is there any way to do it?

4

1 回答 1

2

不知道SpringMVC。但是,如果您想记录每个请求,您可以编写一个日志记录Intent

object RequestLogging {
  def apply[A, B](intent: Cycle.Intent[A, B]) =
    Cycle.Intent[A, B] {
      case req =>
        Cycle.Intent.complete(intent)(req) ~> new ResponseFunction[Any]() {
          override def apply[C <: Any](resp: HttpResponse[C]) = {
            println(s"${req.remoteAddr} ${new Date()} ${req.method} ${req.uri} ${resp.underlying.asInstanceOf[Response].getStatus}")
            resp
          }
        }
    }
}

然后通过Intent像这样包装你的电流来使用它:

val plan = new unfiltered.filter.Plan {
  def intent = RequestLogging {
    case GET(Path("/record/1")) => ...
  }
}
于 2014-07-22T01:53:06.917 回答