def logRequest(marker: String): Directive0
def logRequest(marker: String, level: LogLevel): Directive0
def logRequest(show: HttpRequest => String): Directive0
def logRequest(show: HttpRequest => LogEntry): Directive0
def logRequest(magnet: LoggingMagnet[HttpRequest => 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.


Logs the request using the supplied LoggingMagnet[HttpRequest => Unit] using the LoggingAdapterLoggingAdapter of the RequestContextRequestContext. The LoggingMagnet is a wrapped function HttpRequest => Unit that can be implicitly created from the different constructors shown above. These constructors build a LoggingMagnet from these components: Logs the request. The directive is available with the following parameters:

  • A marker to prefix each log message with.
  • A log level.
  • A show function that calculates a string representation for a request.
  • A function that creates a LogEntryLogEntry which is a combination of the elements above.
  • A marker to prefix each log message with.
  • A log level.
  • A function that creates a LogEntryLogEntry which is a combination of the elements above.

It is also possible to use any other function HttpRequest => Unit for logging by wrapping it with LoggingMagnet. See the examples for ways to use the logRequest directive.

Use logResult for logging the response, or logRequestResult for logging both.

To change the logger, wrap this directive by withLog.


source// different possibilities of using logRequest

// The first alternatives use an implicitly available LoggingContext for logging
// marks with "get-user", log with debug level, HttpRequest.toString

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

// logs just the request method at debug level
def requestMethod(req: HttpRequest): String = req.method.name
DebuggingDirectives.logRequest(requestMethod _)

// logs just the request method at info level
def requestMethodAsInfo(req: HttpRequest): LogEntry = LogEntry(req.method.name, Logging.InfoLevel)
DebuggingDirectives.logRequest(requestMethodAsInfo _)

// This one doesn't use the implicit LoggingContext but uses `println` for logging
def printRequestMethod(req: HttpRequest): Unit = println(req.method.name)
val logRequestPrintln = DebuggingDirectives.logRequest(LoggingMagnet(_ => printRequestMethod))

// tests:
Get("/") ~> logRequestPrintln(complete("logged")) ~> check {
  responseAs[String] shouldEqual "logged"
sourceimport static akka.http.javadsl.server.Directives.complete;
import static akka.http.javadsl.server.Directives.get;
import static akka.http.javadsl.server.Directives.logRequest;

// logs request with "get-user"
final Route routeBasicLogRequest = get(() ->
  logRequest("get-user", () -> complete("logged")));

// logs request with "get-user" as Info
final Route routeBasicLogRequestAsInfo = get(() ->
  logRequest("get-user", InfoLevel(), () -> complete("logged")));

// logs just the request method at info level
Function<HttpRequest, LogEntry> requestMethodAsInfo = (request) ->
  LogEntry.create(request.method().name(), InfoLevel());

final Route routeUsingFunction = get(() ->
  logRequest(requestMethodAsInfo, () -> complete("logged")));

// tests:
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.