Thank you Konrad.

>f you care about efficiency please use the log.warning("[{}] with [{}]", 
req.uri, req.method.value)
Oh. Nice!. Will start to use this pattern for logging. And this is a good 
reminder for me to finish up on Scala Days 2016 :).

On Thursday, 22 December 2016 02:02:11 UTC-8, Konrad Malawski wrote:
>
> Glad you found it.
> If you care about efficiency please use the log.warning("[{}] with [{}]", 
> req.uri, req.method.value) style of logging, it does not needlessly 
> allocate/build strings if a given log level is disabled.
> Also mentioned as one of my akka tips in the zen of akka talk: 
> https://www.youtube.com/watch?v=vgFoKOxrTzg
>
> happy hakking!
>
> -- 
> Konrad `ktoso` Malawski
> Akka <http://akka.io> @ Lightbend <http://lightbend.com>
>
> On 21 December 2016 at 22:55:30, Muthu Jayakumar ([email protected] 
> <javascript:>) wrote:
>
> 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 [email protected] <javascript:>.
> To post to this group, send email to [email protected] 
> <javascript:>.
> Visit this group at https://groups.google.com/group/akka-user.
> For more options, visit https://groups.google.com/d/optout.
>
>

-- 
>>>>>>>>>>      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 [email protected].
To post to this group, send email to [email protected].
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