logResult

Signature

def logResult(marker: String): Directive0
def logResult(marker: String, level: LogLevel): Directive0
def logResult(show: RouteResult => String): Directive0
def logResult(show: RouteResult => LogEntry): Directive0
def logResult(magnet: LoggingMagnet[RouteResult => Unit]): 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 using the LoggingAdapterLoggingAdapter of the RequestContextRequestContext.

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

Scala
source// 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"
}
Java
sourceimport static akka.http.javadsl.server.Directives.logResult;

// logs result with "get-user"
final Route routeBasicLogResult = get(() ->
  logResult("get-user", () -> complete("logged")));

// logs result with "get-user" as Info
final Route routeBasicLogResultAsInfo = get(() ->
  logResult("get-user", InfoLevel(), () -> complete("logged")));

// logs the result and the rejections as LogEntry
Function<HttpResponse, LogEntry> showSuccessAsInfo = (response) ->
  LogEntry.create(String.format("Response code '%d'", response.status().intValue()),
    InfoLevel());

Function<List<Rejection>, LogEntry> showRejectionAsInfo = (rejections) ->
  LogEntry.create(
    rejections
      .stream()
      .map(rejection -> rejection.toString())
      .collect(Collectors.joining(", ")),
    InfoLevel());

final Route routeUsingFunction = get(() ->
  logResult(showSuccessAsInfo, showRejectionAsInfo, () -> complete("logged")));
// tests:
testRoute(routeBasicLogResult).run(HttpRequest.GET("/"))
  .assertEntity("logged");
Found an error in this documentation? The source code for this page can be found here. Please feel free to edit and contribute a pull request.