logResult

Signature

def logResult(marker: String)(implicit log: LoggingContext): Directive0
def logResult(marker: String, level: LogLevel)(implicit log: LoggingContext): Directive0
def logResult(show: RouteResult => String)(implicit log: LoggingContext): Directive0
def logResult(show: RouteResult => LogEntry)(implicit log: LoggingContext): Directive0
def logResult(magnet: LoggingMagnet[RouteResult => Unit])(implicit log: LoggingContext): Directive0

The signature shown is simplified, the real signature uses magnets. [1]

[1] See The Magnet Pattern for an explanation of magnet-based overloading.

Description

Logs the response.

See logRequest for the general description how these directives work. This directive is different as it requires a LoggingMagnet[RouteResult => Unit]. Instead of just logging HttpResponses, logResult is able to log any RouteResult coming back from the inner route.

Use logRequest for logging the request, or logRequestResult for logging both.

Example

// different possibilities of using logResponse

// The first alternatives use an implicitly available LoggingContext for logging
// marks with "get-user", log with debug level, HttpResponse.toString
DebuggingDirectives.logResult("get-user")

// marks with "get-user", log with info level, HttpResponse.toString
DebuggingDirectives.logResult(("get-user", Logging.InfoLevel))

// logs just the response status at debug level
def responseStatus(res: RouteResult): String = res match {
  case RouteResult.Complete(x)          => x.status.toString
  case RouteResult.Rejected(rejections) => "Rejected: " + rejections.mkString(", ")
}
DebuggingDirectives.logResult(responseStatus _)

// logs just the response status at info level
def responseStatusAsInfo(res: RouteResult): LogEntry = LogEntry(responseStatus(res), Logging.InfoLevel)
DebuggingDirectives.logResult(responseStatusAsInfo _)

// This one doesn't use the implicit LoggingContext but uses `println` for logging
def printResponseStatus(res: RouteResult): Unit = println(responseStatus(res))
val logResultPrintln = DebuggingDirectives.logResult(LoggingMagnet(_ => printResponseStatus))

// tests:
Get("/") ~> logResultPrintln(complete("logged")) ~> check {
  responseAs[String] shouldEqual "logged"
}
The source code for this page can be found here.