|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Nutgram\Laravel\Log; |
| 4 | + |
| 5 | +use Monolog\Formatter\FormatterInterface; |
| 6 | +use Monolog\LogRecord; |
| 7 | +use function Termwind\{render}; |
| 8 | + |
| 9 | +class NutgramFormatter implements FormatterInterface |
| 10 | +{ |
| 11 | + public function format(LogRecord $record): void |
| 12 | + { |
| 13 | + $record->context['type'] === 'request' ? $this->formatRequest($record) : $this->formatResponse($record); |
| 14 | + } |
| 15 | + |
| 16 | + public function formatBatch(array $records): void |
| 17 | + { |
| 18 | + array_walk($records, [$this, 'format']); |
| 19 | + } |
| 20 | + |
| 21 | + public function formatRequest(LogRecord $record): void |
| 22 | + { |
| 23 | + $content = json_encode($record->context['content'], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); |
| 24 | + |
| 25 | + render(view('logging::request', [ |
| 26 | + 'time' => $record->datetime->format('Y-m-d H:i:s'), |
| 27 | + 'message' => $record->message, |
| 28 | + 'endpoint' => $record->context['endpoint'], |
| 29 | + 'content' => $content, |
| 30 | + ])); |
| 31 | + } |
| 32 | + |
| 33 | + public function formatResponse(LogRecord $record): void |
| 34 | + { |
| 35 | + $response = json_encode($record->context['response'], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); |
| 36 | + |
| 37 | + render(view('logging::response', [ |
| 38 | + 'time' => $record->datetime->format('Y-m-d H:i:s'), |
| 39 | + 'message' => $record->message, |
| 40 | + 'response' => $response, |
| 41 | + ])); |
| 42 | + } |
| 43 | +} |
0 commit comments