|
2 | 2 |
|
3 | 3 | namespace Bilfeldt\LaravelHttpClientLogger;
|
4 | 4 |
|
5 |
| -use GuzzleHttp\Psr7\Message; |
6 | 5 | use Illuminate\Support\Arr;
|
7 | 6 | use Illuminate\Support\Facades\Log;
|
8 |
| -use Psr\Http\Message\MessageInterface; |
| 7 | +use Illuminate\Support\Facades\Storage; |
| 8 | +use Illuminate\Support\Str; |
9 | 9 | use Psr\Http\Message\RequestInterface;
|
10 | 10 | use Psr\Http\Message\ResponseInterface;
|
11 | 11 |
|
12 | 12 | class HttpLogger implements HttpLoggerInterface
|
13 | 13 | {
|
| 14 | + protected PsrMessageToStringConverter $psrMessageStringConverter; |
| 15 | + |
| 16 | + protected RequestInterface $request; |
| 17 | + protected ResponseInterface $response; |
| 18 | + protected float $sec; |
| 19 | + protected array $context; |
| 20 | + protected array $replace; |
| 21 | + protected string $filename; |
| 22 | + protected string $fileExt = '.txt'; |
| 23 | + |
| 24 | + public function __construct(PsrMessageToStringConverter $psrMessageStringConverter) |
| 25 | + { |
| 26 | + $this->psrMessageStringConverter = $psrMessageStringConverter; |
| 27 | + } |
| 28 | + |
14 | 29 | public function log(
|
15 | 30 | RequestInterface $request,
|
16 | 31 | ResponseInterface $response,
|
17 | 32 | float $sec,
|
18 | 33 | array $context = []
|
19 |
| - ): void { |
20 |
| - $message = "Time {$sec}sec\r\n" |
21 |
| - ."Request\r\n" |
22 |
| - .$this->messageToString($request, Arr::get($context, 'replace', []))."\r\n" |
23 |
| - ."Response\r\n" |
24 |
| - .$this->messageToString($response, Arr::get($context, 'replace', [])); |
| 34 | + ): void |
| 35 | + { |
| 36 | + $this->request = $request; |
| 37 | + $this->response = $response; |
| 38 | + $this->sec = $sec; |
| 39 | + $this->replace = $this->getReplace($context); |
| 40 | + $this->filename = $this->getFileName($context); |
| 41 | + $this->context = $this->getContextCleaned($context); // must be called after the two above since the $context array is modified |
| 42 | + |
| 43 | + if (config('http-client-logger.log_to_channel.enabled')) { |
| 44 | + $this->logToChannel(config('http-client-logger.log_to_channel.channel') ?? config('logging.default')); |
| 45 | + } |
| 46 | + |
| 47 | + if (config('http-client-logger.log_to_disk.enabled')) { |
| 48 | + $this->logToDisk(config('http-client-logger.log_to_disk.disk') ?? config('filesystems.default')); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + protected function getContextCleaned(array $context): array |
| 53 | + { |
| 54 | + return Arr::except($context, ['replace', 'filename']); |
| 55 | + } |
25 | 56 |
|
26 |
| - $context = Arr::except($context, 'replace'); |
| 57 | + protected function getReplace(array $context): array |
| 58 | + { |
| 59 | + return Arr::get($context, 'replace', []); |
| 60 | + } |
| 61 | + |
| 62 | + protected function getFileName(array $context): string |
| 63 | + { |
| 64 | + return Arr::get($context, 'filename', now()->format('Y-m-d-Hisu')); |
| 65 | + } |
27 | 66 |
|
28 |
| - $channel = config('http-client-logger.log_channel') ?? config('logging.default'); |
| 67 | + protected function getMessage(): string |
| 68 | + { |
| 69 | + return "Time {$this->sec}sec\r\n" |
| 70 | + ."Request\r\n" |
| 71 | + .$this->psrMessageStringConverter->toString($this->request, $this->replace)."\r\n" |
| 72 | + ."Response\r\n" |
| 73 | + .$this->psrMessageStringConverter->toString($this->response, $this->replace); |
| 74 | + } |
29 | 75 |
|
30 |
| - if ($response->getStatusCode() >= 200 && $response->getStatusCode() < 300) { |
31 |
| - Log::channel($channel)->debug($message, $context); |
32 |
| - } elseif ($response->getStatusCode() >= 300 && $response->getStatusCode() < 400) { |
33 |
| - Log::channel($channel)->info($message, $context); |
34 |
| - } elseif ($response->getStatusCode() >= 400 && $response->getStatusCode() < 500) { |
35 |
| - Log::channel($channel)->error($message, $context); |
| 76 | + protected function logToChannel(string $channel): void |
| 77 | + { |
| 78 | + if ($this->response->getStatusCode() >= 200 && $this->response->getStatusCode() < 300) { |
| 79 | + Log::channel($channel)->debug($this->getMessage(), $this->context); |
| 80 | + } elseif ($this->response->getStatusCode() >= 300 && $this->response->getStatusCode() < 400) { |
| 81 | + Log::channel($channel)->info($this->getMessage(), $this->context); |
| 82 | + } elseif ($this->response->getStatusCode() >= 400 && $this->response->getStatusCode() < 500) { |
| 83 | + Log::channel($channel)->error($this->getMessage(), $this->context); |
36 | 84 | } else {
|
37 |
| - Log::channel($channel)->critical($message, $context); |
| 85 | + Log::channel($channel)->critical($this->getMessage(), $this->context); |
38 | 86 | }
|
39 | 87 | }
|
40 | 88 |
|
41 |
| - protected function messageToString(MessageInterface $message, array $placeholders): string |
| 89 | + protected function logToDisk(string $disk): void |
42 | 90 | {
|
43 |
| - return strtr(Message::toString($message), $placeholders); |
| 91 | + if (config('http-client-logger.log_to_disk.separate')) { |
| 92 | + Storage::disk($disk)->put( |
| 93 | + $this->filename.'-request'.Str::start($this->fileExt, '.'), |
| 94 | + $this->psrMessageStringConverter->toString($this->request, $this->replace) |
| 95 | + ); |
| 96 | + Storage::disk($disk)->put( |
| 97 | + $this->filename.'-response'.Str::start($this->fileExt, '.'), |
| 98 | + $this->psrMessageStringConverter->toString($this->response, $this->replace) |
| 99 | + ); |
| 100 | + } else { |
| 101 | + Storage::disk($disk)->put( |
| 102 | + $this->filename.Str::start($this->fileExt, '.'), |
| 103 | + $this->getMessage() |
| 104 | + ); |
| 105 | + } |
44 | 106 | }
|
45 | 107 | }
|
0 commit comments