You are here

public static function Middleware::log in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 vendor/guzzlehttp/guzzle/src/Middleware.php \GuzzleHttp\Middleware::log()

Middleware that logs requests, responses, and errors using a message formatter.

Parameters

LoggerInterface $logger Logs messages.:

MessageFormatter $formatter Formatter used to create message strings.:

string $logLevel Level at which to log requests.:

Return value

callable Returns a function that accepts the next handler.

File

vendor/guzzlehttp/guzzle/src/Middleware.php, line 184

Class

Middleware
Functions used to create and wrap handlers with handler middleware.

Namespace

GuzzleHttp

Code

public static function log(LoggerInterface $logger, MessageFormatter $formatter, $logLevel = LogLevel::INFO) {
  return function (callable $handler) use ($logger, $formatter, $logLevel) {
    return function ($request, array $options) use ($handler, $logger, $formatter, $logLevel) {
      return $handler($request, $options)
        ->then(function ($response) use ($logger, $request, $formatter, $logLevel) {
        $message = $formatter
          ->format($request, $response);
        $logger
          ->log($logLevel, $message);
        return $response;
      }, function ($reason) use ($logger, $request, $formatter) {
        $response = $reason instanceof RequestException ? $reason
          ->getResponse() : null;
        $message = $formatter
          ->format($request, $response, $reason);
        $logger
          ->notice($message);
        return \GuzzleHttp\Promise\rejection_for($reason);
      });
    };
  };
}