* [#21090] Added the improved code for loggingResponsetime and added the link in the docs * [#21090] Added javadocs for logRequestResponseTime * [#21090] Removed the compilation error caused by the missing imports * [#21090] Removed the compilation error caused by type * [#21090] Improved the identation of the code * [#21090] Improved the identation of the code * [#21090] Removed the empty line * [#21090] Refactored the documentation with clear explanations
This commit is contained in:
parent
d1a9049fa0
commit
25e4586aa0
4 changed files with 119 additions and 23 deletions
|
|
@ -4,9 +4,11 @@
|
|||
|
||||
package docs.http.scaladsl.server.directives
|
||||
|
||||
import akka.event.Logging
|
||||
import akka.event.{LoggingAdapter, Logging}
|
||||
import akka.event.Logging.LogLevel
|
||||
import akka.http.scaladsl.model.{ HttpRequest, HttpResponse }
|
||||
import akka.http.scaladsl.server.RouteResult
|
||||
import akka.http.scaladsl.server.RouteResult.{Rejected, Complete}
|
||||
import akka.http.scaladsl.server.directives.{ DebuggingDirectives, LogEntry, LoggingMagnet }
|
||||
import docs.http.scaladsl.server.RoutingSpec
|
||||
|
||||
|
|
@ -95,4 +97,31 @@ class DebuggingDirectivesExamplesSpec extends RoutingSpec {
|
|||
responseAs[String] shouldEqual "logged"
|
||||
}
|
||||
}
|
||||
"logRequestResultWithResponseTime" in {
|
||||
|
||||
def akkaResponseTimeLoggingFunction(loggingAdapter: LoggingAdapter,
|
||||
requestTimestamp: Long,
|
||||
level: LogLevel = Logging.InfoLevel)(req: HttpRequest)(res: Any): Unit = {
|
||||
val entry = res match {
|
||||
case Complete(resp) =>
|
||||
val responseTimestamp: Long = System.nanoTime
|
||||
val elapsedTime: Long = (responseTimestamp - requestTimestamp) / 1000000
|
||||
val loggingString = s"""Logged Request:${req.method}:${req.uri}:${resp.status}:${elapsedTime}"""
|
||||
LogEntry(loggingString, level)
|
||||
case Rejected(reason) =>
|
||||
LogEntry(s"Rejected Reason: ${reason.mkString(",")}", level)
|
||||
}
|
||||
entry.logTo(loggingAdapter)
|
||||
}
|
||||
def printResponseTime(log: LoggingAdapter) = {
|
||||
val requestTimestamp = System.nanoTime
|
||||
akkaResponseTimeLoggingFunction(log, requestTimestamp)(_)
|
||||
}
|
||||
|
||||
val logResponseTime = DebuggingDirectives.logRequestResult(LoggingMagnet(printResponseTime(_)))
|
||||
|
||||
Get("/") ~> logResponseTime(complete("logged")) ~> check {
|
||||
responseAs[String] shouldEqual "logged"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue