1111use Psr \Log \LoggerInterface ;
1212
1313/**
14- * Log request, response and exception for a HTTP Client.
14+ * Log request, response and exception for an HTTP Client.
1515 *
1616 * @author Joel Wurtz <joel.wurtz@gmail.com>
1717 */
1818final class LoggerPlugin implements Plugin
1919{
20- /**
21- * Logger to log request / response / exception for a http call.
22- *
23- * @var LoggerInterface
24- */
2520 private $ logger ;
26-
27- /**
28- * Formats a request/response as string.
29- *
30- * @var Formatter
31- */
3221 private $ formatter ;
3322
34- /**
35- * @param LoggerInterface $logger
36- */
3723 public function __construct (LoggerInterface $ logger , Formatter $ formatter = null )
3824 {
3925 $ this ->logger = $ logger ;
@@ -45,11 +31,11 @@ public function __construct(LoggerInterface $logger, Formatter $formatter = null
4531 */
4632 public function handleRequest (RequestInterface $ request , callable $ next , callable $ first )
4733 {
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 ]);
4935
5036 return $ next ($ request )->then (function (ResponseInterface $ response ) use ($ request ) {
5137 $ 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 )),
5339 [
5440 'request ' => $ request ,
5541 'response ' => $ response ,
@@ -60,7 +46,7 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
6046 }, function (Exception $ exception ) use ($ request ) {
6147 if ($ exception instanceof Exception \HttpException) {
6248 $ 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 )),
6450 [
6551 'request ' => $ request ,
6652 'response ' => $ exception ->getResponse (),
@@ -69,7 +55,7 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
6955 );
7056 } else {
7157 $ 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 )),
7359 [
7460 'request ' => $ request ,
7561 'exception ' => $ exception ,
0 commit comments