1
1
<?php
2
2
3
- use Psr \Log \LoggerInterface ;
4
- use Psr \Http \Message \RequestInterface ;
3
+ use function GuzzleHttp \Promise \rejection_for ;
5
4
use Illuminated \Console \Exceptions \RuntimeException ;
5
+ use Psr \Http \Message \RequestInterface ;
6
+ use Psr \Http \Message \ResponseInterface ;
7
+ use Psr \Log \LoggerInterface ;
6
8
7
9
if (!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 )
9
23
{
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
12
27
$ method = (string ) $ request ->getMethod ();
13
28
$ uri = (string ) $ request ->getUri ();
14
29
$ body = (string ) $ request ->getBody ();
15
30
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 )) {
17
36
$ message = "[ {$ method }] Calling ` {$ uri }`. " ;
18
37
$ context = [];
19
38
} else {
@@ -30,55 +49,46 @@ function iclogger_guzzle_middleware(LoggerInterface $log, $type = 'raw', callabl
30
49
break ;
31
50
}
32
51
}
52
+ $ logger ->info ($ message , $ context );
33
53
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
44
55
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
46
58
$ body = (string ) $ response ->getBody ();
47
59
$ code = $ response ->getStatusCode ();
48
60
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 ;
67
74
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 ;
73
84
}
74
85
}
75
-
76
- $ log ->info ($ message , $ context );
86
+ $ logger ->info ($ message , $ context );
77
87
78
88
return $ response ;
79
89
},
80
90
function ($ reason ) {
81
- return \ GuzzleHttp \ Promise \ rejection_for ($ reason );
91
+ return rejection_for ($ reason );
82
92
}
83
93
);
84
94
};
0 commit comments