Browse Source

server: Add LoggingFilter

scalafmt-draft
Alexis Hernandez 7 years ago
parent
commit
34a6277d46
  1. 33
      server/app/com/xsn/explorer/play/LoggingFilter.scala
  2. 2
      server/conf/application.conf

33
server/app/com/xsn/explorer/play/LoggingFilter.scala

@ -0,0 +1,33 @@
package com.xsn.explorer.play
import javax.inject.Inject
import akka.stream.Materializer
import org.slf4j.LoggerFactory
import play.api.mvc.{Filter, RequestHeader, Result}
import scala.concurrent.{ExecutionContext, Future}
class LoggingFilter @Inject() (
implicit val mat: Materializer,
ec: ExecutionContext)
extends Filter {
private val logger = LoggerFactory.getLogger(this.getClass)
def apply(nextFilter: RequestHeader => Future[Result])
(requestHeader: RequestHeader): Future[Result] = {
val startTime = System.currentTimeMillis
nextFilter(requestHeader).map { result =>
val endTime = System.currentTimeMillis
val requestTime = endTime - startTime
logger.info(s"${requestHeader.method} ${requestHeader.uri} took $requestTime ms and returned ${result.header.status}")
result.withHeaders("Request-Time" -> requestTime.toString)
}
}
}

2
server/conf/application.conf

@ -13,6 +13,8 @@ play.http {
secret.key=${?PLAY_APPLICATION_SECRET}
}
play.filters.enabled += "com.xsn.explorer.play.LoggingFilter"
rpc {
host = "http://localhost:51473"
username = "dummy"

Loading…
Cancel
Save