Completed
Pull Request — master (#31)
by Gerardo
01:09
created

DefaultLogResponseWriter::logResponse()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 8.8337
c 0
b 0
f 0
cc 6
nc 8
nop 2
1
<?php
2
3
namespace Spatie\HttpLogger;
4
5
use Exception;
6
use Illuminate\Http\JsonResponse;
7
use Illuminate\Http\Request;
8
use Illuminate\Http\Response as HtmlResponse;
9
use Illuminate\Support\Facades\Log;
10
use Symfony\Component\HttpFoundation\Response;
11
12
class DefaultLogResponseWriter implements LogResponseWriter
13
{
14
    public function logResponse(Request $request, Response $response)
15
    {
16
        $method = strtoupper($request->getMethod());
17
18
        $uri = $request->getPathInfo();
19
20
        $message = "{$method} {$uri} - ResponseBody: {}";
21
22
        try {
23
            if ($response instanceof Response) {
24
                if ($response->getStatusCode() == 200) {
25
                    if ($response instanceof JsonResponse) {
26
                        $body = json_encode($response->getData());
27
                    } elseif ($response instanceof HtmlResponse) {
28
                        $body = $response->getContent();
29
                    } else {
30
                        $body = $response;
31
                    }
32
33
                    $message = "{$method} {$uri} - ResponseBody: {$body}";
34
                } else {
35
                    $message = "{$method} {$uri} - ResponseBodyCode: {$response->getStatusCode()}";
36
                }
37
            }
38
        } catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
39
        }
40
41
        Log::info($message);
42
    }
43
}
44