|
5 | 5 | namespace KaririCode\Logging\Formatter; |
6 | 6 |
|
7 | 7 | use KaririCode\Contract\ImmutableValue; |
| 8 | +use KaririCode\Logging\LogRecord; |
8 | 9 |
|
9 | | -class JsonFormatter extends AbstractFormatter |
| 10 | +/** |
| 11 | + * JSON formatter optimized for direct property access. |
| 12 | + */ |
| 13 | +final class JsonFormatter extends AbstractFormatter |
10 | 14 | { |
11 | | - private const JSON_OPTIONS = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE; |
| 15 | + private const JSON_OPTIONS = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR; |
12 | 16 |
|
| 17 | + /** |
| 18 | + * @param string $dateFormat Date format pattern |
| 19 | + * @param bool $includeContext Include context in output |
| 20 | + * @param bool $includeExtra Include extra data in output |
| 21 | + * @param bool $prettyPrint Pretty print JSON output |
| 22 | + * @param bool $includeStacktraces Include stack traces for exceptions |
| 23 | + */ |
| 24 | + public function __construct( |
| 25 | + string $dateFormat = 'Y-m-d H:i:s', |
| 26 | + bool $includeContext = true, |
| 27 | + bool $includeExtra = false, |
| 28 | + public readonly bool $prettyPrint = false, |
| 29 | + public readonly bool $includeStacktraces = false |
| 30 | + ) { |
| 31 | + parent::__construct($dateFormat, $includeContext, $includeExtra); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Format log record to JSON using direct property access. |
| 36 | + */ |
13 | 37 | public function format(ImmutableValue $record): string |
14 | 38 | { |
15 | | - $data = $this->prepareData($record); |
| 39 | + if (!$record instanceof LogRecord) { |
| 40 | + throw new \InvalidArgumentException('Record must be an instance of LogRecord'); |
| 41 | + } |
| 42 | + |
| 43 | + // Direct property access - no toArray() needed |
| 44 | + $data = [ |
| 45 | + 'datetime' => $this->formatTimestamp($record->datetime), |
| 46 | + 'level' => $record->level->value, |
| 47 | + 'message' => $record->getMessageAsString(), |
| 48 | + ]; |
| 49 | + |
| 50 | + // Conditionally add context and extra |
| 51 | + if ($this->shouldIncludeContext($record->context)) { |
| 52 | + $data['context'] = $this->processContext($record->context); |
| 53 | + } |
| 54 | + |
| 55 | + if ($this->shouldIncludeExtra($record->extra)) { |
| 56 | + $data['extra'] = $record->extra; |
| 57 | + } |
16 | 58 |
|
17 | 59 | return $this->encodeJson($data); |
18 | 60 | } |
19 | 61 |
|
| 62 | + /** |
| 63 | + * Format batch of records. |
| 64 | + */ |
20 | 65 | public function formatBatch(array $records): string |
21 | 66 | { |
22 | | - $formattedRecords = array_map([$this, 'prepareData'], $records); |
| 67 | + $formattedRecords = array_map( |
| 68 | + fn ($record) => $this->prepareData($record), |
| 69 | + $records |
| 70 | + ); |
23 | 71 |
|
24 | 72 | return $this->encodeJson($formattedRecords); |
25 | 73 | } |
26 | 74 |
|
| 75 | + /** |
| 76 | + * Prepare data from record using direct property access. |
| 77 | + */ |
27 | 78 | private function prepareData(ImmutableValue $record): array |
28 | 79 | { |
29 | | - $data = [ |
30 | | - 'datetime' => $record->datetime->format($this->dateFormat), |
| 80 | + if (!$record instanceof LogRecord) { |
| 81 | + throw new \InvalidArgumentException('Record must be an instance of LogRecord'); |
| 82 | + } |
| 83 | + |
| 84 | + return [ |
| 85 | + 'datetime' => $this->formatTimestamp($record->datetime), |
31 | 86 | 'level' => $record->level->value, |
32 | | - 'message' => $record->message, |
| 87 | + 'message' => $record->getMessageAsString(), |
| 88 | + 'context' => $record->hasContext() ? $this->processContext($record->context) : null, |
| 89 | + 'extra' => $record->hasExtra() ? $record->extra : null, |
| 90 | + ]; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Process context to handle exceptions if needed. |
| 95 | + */ |
| 96 | + private function processContext(array $context): array |
| 97 | + { |
| 98 | + if (!$this->includeStacktraces) { |
| 99 | + return $context; |
| 100 | + } |
| 101 | + |
| 102 | + // Handle exceptions in context |
| 103 | + foreach ($context as $key => $value) { |
| 104 | + if ($value instanceof \Throwable) { |
| 105 | + $context[$key] = $this->formatException($value); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + return $context; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Format exception for JSON output. |
| 114 | + */ |
| 115 | + private function formatException(\Throwable $exception): array |
| 116 | + { |
| 117 | + $formatted = [ |
| 118 | + 'class' => get_class($exception), |
| 119 | + 'message' => $exception->getMessage(), |
| 120 | + 'code' => $exception->getCode(), |
| 121 | + 'file' => $exception->getFile(), |
| 122 | + 'line' => $exception->getLine(), |
33 | 123 | ]; |
34 | 124 |
|
35 | | - if (!empty($record->context)) { |
36 | | - $data['context'] = $record->context; |
| 125 | + if ($this->includeStacktraces) { |
| 126 | + $formatted['trace'] = $exception->getTraceAsString(); |
| 127 | + } |
| 128 | + |
| 129 | + if ($exception->getPrevious()) { |
| 130 | + $formatted['previous'] = $this->formatException($exception->getPrevious()); |
37 | 131 | } |
38 | 132 |
|
39 | | - return $data; |
| 133 | + return $formatted; |
40 | 134 | } |
41 | 135 |
|
42 | | - private function encodeJson($data): string |
| 136 | + /** |
| 137 | + * Encode data to JSON with configured options. |
| 138 | + */ |
| 139 | + private function encodeJson(mixed $data): string |
43 | 140 | { |
44 | | - return json_encode($data, self::JSON_OPTIONS | JSON_THROW_ON_ERROR); |
| 141 | + $options = self::JSON_OPTIONS; |
| 142 | + |
| 143 | + if ($this->prettyPrint) { |
| 144 | + $options |= JSON_PRETTY_PRINT; |
| 145 | + } |
| 146 | + |
| 147 | + return json_encode($data, $options); |
45 | 148 | } |
46 | 149 | } |
0 commit comments