11
11
use Psr \Log \LoggerInterface ;
12
12
13
13
/**
14
- * Log request, response and exception for a HTTP Client.
14
+ * Log request, response and exception for an HTTP Client.
15
15
*
16
16
* @author Joel Wurtz <joel.wurtz@gmail.com>
17
17
*/
18
18
final class LoggerPlugin implements Plugin
19
19
{
20
- /**
21
- * Logger to log request / response / exception for a http call.
22
- *
23
- * @var LoggerInterface
24
- */
25
20
private $ logger ;
26
-
27
- /**
28
- * Formats a request/response as string.
29
- *
30
- * @var Formatter
31
- */
32
21
private $ formatter ;
33
22
34
- /**
35
- * @param LoggerInterface $logger
36
- */
37
23
public function __construct (LoggerInterface $ logger , Formatter $ formatter = null )
38
24
{
39
25
$ this ->logger = $ logger ;
@@ -45,11 +31,11 @@ public function __construct(LoggerInterface $logger, Formatter $formatter = null
45
31
*/
46
32
public function handleRequest (RequestInterface $ request , callable $ next , callable $ first )
47
33
{
48
- $ this ->logger ->info (sprintf (' Emit request: " %s"' , $ this ->formatter ->formatRequest ($ request )), ['request ' => $ request ]);
34
+ $ this ->logger ->info (sprintf (" Sending request:\n %s " , $ this ->formatter ->formatRequest ($ request )), ['request ' => $ request ]);
49
35
50
36
return $ next ($ request )->then (function (ResponseInterface $ response ) use ($ request ) {
51
37
$ this ->logger ->info (
52
- sprintf (' Receive response: "%s" for request: " %s"' , $ this ->formatter ->formatResponse ($ response ), $ this ->formatter ->formatRequest ($ request )),
38
+ sprintf (" Received response:\n %s \n\n for request:\n %s " , $ this ->formatter ->formatResponse ($ response ), $ this ->formatter ->formatRequest ($ request )),
53
39
[
54
40
'request ' => $ request ,
55
41
'response ' => $ response ,
@@ -60,7 +46,7 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
60
46
}, function (Exception $ exception ) use ($ request ) {
61
47
if ($ exception instanceof Exception \HttpException) {
62
48
$ this ->logger ->error (
63
- sprintf (' Error: "%s" with response: "%s" when emitting request: " %s" ' , $ exception ->getMessage (), $ this ->formatter ->formatResponse ($ exception ->getResponse ()), $ this ->formatter ->formatRequest ($ request )),
49
+ sprintf (" Error: \n %s \n with response:\n %s \n\n when sending request:\n %s " , $ exception ->getMessage (), $ this ->formatter ->formatResponse ($ exception ->getResponse ()), $ this ->formatter ->formatRequest ($ request )),
64
50
[
65
51
'request ' => $ request ,
66
52
'response ' => $ exception ->getResponse (),
@@ -69,7 +55,7 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
69
55
);
70
56
} else {
71
57
$ this ->logger ->error (
72
- sprintf (' Error: "%s" when emitting request: " %s" ' , $ exception ->getMessage (), $ this ->formatter ->formatRequest ($ request )),
58
+ sprintf (" Error: \n %s \n when sending request:\n %s " , $ exception ->getMessage (), $ this ->formatter ->formatRequest ($ request )),
73
59
[
74
60
'request ' => $ request ,
75
61
'exception ' => $ exception ,
0 commit comments