function Middleware::log
Middleware that logs requests, responses, and errors using a message formatter.
@phpstan-param \Psr\Log\LogLevel::* $logLevel Level at which to log requests.
Parameters
LoggerInterface $logger Logs messages.:
MessageFormatterInterface|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 198
Class
- Middleware
- Functions used to create and wrap handlers with handler middleware.
Namespace
GuzzleHttpCode
public static function log(LoggerInterface $logger, $formatter, string $logLevel = 'info') : callable {
// To be compatible with Guzzle 7.1.x we need to allow users to pass a MessageFormatter
if (!$formatter instanceof MessageFormatter && !$formatter instanceof MessageFormatterInterface) {
throw new \LogicException(sprintf('Argument 2 to %s::log() must be of type %s', self::class, MessageFormatterInterface::class));
}
return static function (callable $handler) use ($logger, $formatter, $logLevel) : callable {
return static function (RequestInterface $request, array $options = []) use ($handler, $logger, $formatter, $logLevel) {
return $handler($request, $options)->then(static function ($response) use ($logger, $request, $formatter, $logLevel) : ResponseInterface {
$message = $formatter->format($request, $response);
$logger->log($logLevel, $message);
return $response;
}, static function ($reason) use ($logger, $request, $formatter) : PromiseInterface {
$response = $reason instanceof RequestException ? $reason->getResponse() : null;
$message = $formatter->format($request, $response, P\Create::exceptionFor($reason));
$logger->error($message);
return P\Create::rejectionFor($reason);
});
};
};
}