For anyone who wants to do something similar here is how i have done with 
akka-http 10.

DebuggingDirectives.logRequestResult(LoggingMagnet(_ => clockHTTPTime(log)))

private def clockHTTPTime(log: LoggingAdapter)(req: HttpRequest): (Any) => Unit 
= {
  def clockHTTPTimeInternal(log: LoggingAdapter, req: HttpRequest, 
requestTimestamp: Long)(res: Any): Unit = {
    res match {
      case Complete(resp) => log.info(s"""[${req.uri}] with 
[${req.method.value} - ${resp.status}] - Completed in 
${DateUtils.prettyPrintTimeTaken(requestTimestamp)}""")
      case Rejected(reason) =>  log.warning(s"[${req.uri}] with 
[${req.method.value}] - Rejected Reason: ${reason.mkString(",")}") 
    }
  }
  val requestTimestamp = System.currentTimeMillis()
  clockHTTPTimeInternal(log, req, requestTimestamp)(_)
}


Reference: 
http://doc.akka.io/docs/akka-http/current/scala/http/routing-dsl/directives/debugging-directives/logRequestResult.html

Well documented.

Thanks,
Muthu


On Sunday, 18 December 2016 12:30:19 UTC-8, Muthu Jayakumar wrote:
>
> Hello there,
>
> I have used spray-io in the past and am new to akka-http. I have a 
> question on writing directive/route that helps to intercept some / all 
> end-points to write some useful logging (for now and may be add some 
> headers and other stuff later). 
> While I was writing code with spray-io I had..
>
> def clockTimeTaken: Directive0 = {
>   mapRequestContext { ctx =>
>     val startTime = System.currentTimeMillis()
>     log.mdc(Map("httpUrl" -> ctx.request.uri.toRelative.toString(), 
> "httpMethod" -> ctx.request.method.name)) //add to actor logging.
>     log.info(s"Started to service request")
>     ctx.withHttpResponseMapped { response =>
>       log.info(s"Completed HTTP in 
> ${DateUtils.prettyPrintTimeTaken(startTime)} [${response.messagePart}]")
>       log.clearMDC()
>       response
>     }
>   }
> }
>
>
> Any pointers on how I could rewrite this would help.
>
> Thanks,
> Muthu
>

-- 
>>>>>>>>>>      Read the docs: http://akka.io/docs/
>>>>>>>>>>      Check the FAQ: 
>>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>>      Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to akka-user+unsubscr...@googlegroups.com.
To post to this group, send email to akka-user@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to