11<?php
22
3- use Psr \Log \LoggerInterface ;
4- use Psr \Http \Message \RequestInterface ;
3+ use function GuzzleHttp \Promise \rejection_for ;
54use Illuminated \Console \Exceptions \RuntimeException ;
5+ use Psr \Http \Message \RequestInterface ;
6+ use Psr \Http \Message \ResponseInterface ;
7+ use Psr \Log \LoggerInterface ;
68
79if (!function_exists ('iclogger_guzzle_middleware ' )) {
8- function iclogger_guzzle_middleware (LoggerInterface $ log , $ type = 'raw ' , callable $ shouldLogRequest = null , callable $ shouldLogResponse = null )
10+ /**
11+ * Create a Guzzle middleware to provide logging of Guzzle requests/responses.
12+ *
13+ * @see https://github.com/dmitry-ivanov/laravel-console-logger#guzzle-6-integration
14+ * @see http://docs.guzzlephp.org/en/stable/handlers-and-middleware.html
15+ *
16+ * @param \Psr\Log\LoggerInterface $logger
17+ * @param string $type
18+ * @param callable|null $shouldLogRequest
19+ * @param callable|null $shouldLogResponse
20+ * @return \Closure
21+ */
22+ function iclogger_guzzle_middleware (LoggerInterface $ logger , string $ type = 'raw ' , callable $ shouldLogRequest = null , callable $ shouldLogResponse = null )
923 {
10- return function (callable $ handler ) use ($ log , $ type , $ shouldLogRequest , $ shouldLogResponse ) {
11- return function (RequestInterface $ request , array $ options ) use ($ handler , $ log , $ type , $ shouldLogRequest , $ shouldLogResponse ) {
24+ return function (callable $ handler ) use ($ logger , $ type , $ shouldLogRequest , $ shouldLogResponse ) {
25+ return function (RequestInterface $ request , array $ options ) use ($ handler , $ logger , $ type , $ shouldLogRequest , $ shouldLogResponse ) {
26+ // Gather information about the request
1227 $ method = (string ) $ request ->getMethod ();
1328 $ uri = (string ) $ request ->getUri ();
1429 $ body = (string ) $ request ->getBody ();
1530
16- if (empty ($ body )) {
31+ // Log the request with a proper message and context
32+ if (isset ($ shouldLogRequest ) && !$ shouldLogRequest ($ request )) {
33+ $ message = "[ {$ method }] Calling ` {$ uri }`, body is not shown, according to the custom logic. " ;
34+ $ context = [];
35+ } elseif (empty ($ body )) {
1736 $ message = "[ {$ method }] Calling ` {$ uri }`. " ;
1837 $ context = [];
1938 } else {
@@ -30,55 +49,46 @@ function iclogger_guzzle_middleware(LoggerInterface $log, $type = 'raw', callabl
3049 break ;
3150 }
3251 }
52+ $ logger ->info ($ message , $ context );
3353
34- if (!empty ($ shouldLogRequest )) {
35- $ shouldLogRequest = call_user_func ($ shouldLogRequest , $ request );
36- if (!$ shouldLogRequest ) {
37- $ message = "[ {$ method }] Calling ` {$ uri }`, body is not shown, according to the custom logic. " ;
38- $ context = [];
39- }
40- }
41-
42- $ log ->info ($ message , $ context );
43-
54+ // Using another callback to log the response
4455 return $ handler ($ request , $ options )->then (
45- function ($ response ) use ($ request , $ log , $ type , $ shouldLogResponse ) {
56+ function (ResponseInterface $ response ) use ($ request , $ logger , $ type , $ shouldLogResponse ) {
57+ // Gather information about the response
4658 $ body = (string ) $ response ->getBody ();
4759 $ code = $ response ->getStatusCode ();
4860
49- $ message = "[ {$ code }] Response: " ;
50- switch ($ type ) {
51- case 'json ' :
52- $ context = is_json ($ body , true );
53- if (empty ($ context )) {
54- throw new RuntimeException ('Bad response, json expected. ' , ['response ' => $ body ]);
55- }
56- break ;
57-
58- case 'raw ' :
59- default :
60- $ message .= "\n{$ body }" ;
61- $ context = [];
62- break ;
63- }
64- if (!empty ($ context )) {
65- $ response ->iclParsedBody = $ context ;
66- }
61+ // Log the response with a proper message and context
62+ if (isset ($ shouldLogResponse ) && !$ shouldLogResponse ($ request , $ response )) {
63+ $ message = "[ {$ code }] Response is not shown, according to the custom logic. " ;
64+ $ context = [];
65+ } else {
66+ $ message = "[ {$ code }] Response: " ;
67+ switch ($ type ) {
68+ case 'json ' :
69+ $ context = is_json ($ body , true );
70+ if (empty ($ context )) {
71+ throw new RuntimeException ('Bad response, JSON expected. ' , ['response ' => $ body ]);
72+ }
73+ break ;
6774
68- if (!empty ($ shouldLogResponse )) {
69- $ shouldLogResponse = call_user_func ($ shouldLogResponse , $ request , $ response );
70- if (!$ shouldLogResponse ) {
71- $ message = "[ {$ code }] Response is not shown, according to the custom logic. " ;
72- $ context = [];
75+ case 'raw ' :
76+ default :
77+ $ message .= "\n{$ body }" ;
78+ $ context = [];
79+ break ;
80+ }
81+ // Save the parsed body of response, so that it could be re-used instead of double decoding
82+ if (!empty ($ context )) {
83+ $ response ->iclParsedBody = $ context ;
7384 }
7485 }
75-
76- $ log ->info ($ message , $ context );
86+ $ logger ->info ($ message , $ context );
7787
7888 return $ response ;
7989 },
8090 function ($ reason ) {
81- return \ GuzzleHttp \ Promise \ rejection_for ($ reason );
91+ return rejection_for ($ reason );
8292 }
8393 );
8494 };
0 commit comments