|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Http\Client\Plugin; |
|
4
|
|
|
|
|
5
|
1 |
|
@trigger_error('The '.__NAMESPACE__.'\LoggerPlugin class is deprecated since version 1.1 and will be removed in 2.0. Use Http\Client\Common\Plugin\LoggerPlugin instead.', E_USER_DEPRECATED); |
|
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
use Http\Client\Exception; |
|
8
|
|
|
use Http\Message\Formatter; |
|
9
|
|
|
use Http\Message\Formatter\SimpleFormatter; |
|
10
|
|
|
use Psr\Http\Message\RequestInterface; |
|
11
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
12
|
|
|
use Psr\Log\LoggerInterface; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Log request, response and exception for a HTTP Client. |
|
16
|
|
|
* |
|
17
|
|
|
* @author Joel Wurtz <[email protected]> |
|
18
|
|
|
* |
|
19
|
|
|
* @deprecated since since version 1.1, and will be removed in 2.0. Use {@link \Http\Client\Common\Plugin\LoggerPlugin} instead. |
|
20
|
|
|
*/ |
|
21
|
|
|
class LoggerPlugin implements Plugin |
|
|
|
|
|
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* Logger to log request / response / exception for a http call. |
|
25
|
|
|
* |
|
26
|
|
|
* @var LoggerInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
private $logger; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Formats a request/response as string. |
|
32
|
|
|
* |
|
33
|
|
|
* @var Formatter |
|
34
|
|
|
*/ |
|
35
|
|
|
private $formatter; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param LoggerInterface $logger |
|
39
|
|
|
*/ |
|
40
|
5 |
|
public function __construct(LoggerInterface $logger, Formatter $formatter = null) |
|
41
|
|
|
{ |
|
42
|
5 |
|
$this->logger = $logger; |
|
43
|
5 |
|
$this->formatter = $formatter ?: new SimpleFormatter(); |
|
44
|
5 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* {@inheritdoc} |
|
48
|
|
|
*/ |
|
49
|
3 |
|
public function handleRequest(RequestInterface $request, callable $next, callable $first) |
|
50
|
|
|
{ |
|
51
|
3 |
|
$this->logger->info(sprintf('Emit request: "%s"', $this->formatter->formatRequest($request)), ['request' => $request]); |
|
52
|
|
|
|
|
53
|
|
|
return $next($request)->then(function (ResponseInterface $response) use ($request) { |
|
54
|
1 |
|
$this->logger->info( |
|
55
|
1 |
|
sprintf('Receive response: "%s" for request: "%s"', $this->formatter->formatResponse($response), $this->formatter->formatRequest($request)), |
|
56
|
|
|
[ |
|
57
|
1 |
|
'request' => $request, |
|
58
|
1 |
|
'response' => $response, |
|
59
|
|
|
] |
|
60
|
1 |
|
); |
|
61
|
|
|
|
|
62
|
1 |
|
return $response; |
|
63
|
3 |
|
}, function (Exception $exception) use ($request) { |
|
64
|
2 |
|
if ($exception instanceof Exception\HttpException) { |
|
65
|
1 |
|
$this->logger->error( |
|
66
|
1 |
|
sprintf('Error: "%s" with response: "%s" when emitting request: "%s"', $exception->getMessage(), $this->formatter->formatResponse($exception->getResponse()), $this->formatter->formatRequest($request)), |
|
67
|
|
|
[ |
|
68
|
1 |
|
'request' => $request, |
|
69
|
1 |
|
'response' => $exception->getResponse(), |
|
70
|
1 |
|
'exception' => $exception, |
|
71
|
|
|
] |
|
72
|
1 |
|
); |
|
73
|
1 |
|
} else { |
|
74
|
1 |
|
$this->logger->error( |
|
75
|
1 |
|
sprintf('Error: "%s" when emitting request: "%s"', $exception->getMessage(), $this->formatter->formatRequest($request)), |
|
76
|
|
|
[ |
|
77
|
1 |
|
'request' => $request, |
|
78
|
1 |
|
'exception' => $exception, |
|
79
|
|
|
] |
|
80
|
1 |
|
); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
2 |
|
throw $exception; |
|
84
|
3 |
|
}); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: