|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace Bref\Monolog; |
| 4 | + |
| 5 | +use ArrayObject; |
| 6 | +use DateTimeInterface; |
| 7 | +use JsonSerializable; |
| 8 | +use Monolog\Formatter\NormalizerFormatter; |
| 9 | +use Monolog\LogRecord; |
| 10 | +use Stringable; |
| 11 | +use Throwable; |
| 12 | + |
| 13 | +/** |
| 14 | + * Monolog formatter optimized for CloudWatch logs. |
| 15 | + */ |
| 16 | +class CloudWatchFormatter extends NormalizerFormatter |
| 17 | +{ |
| 18 | + public function format(LogRecord $record): string |
| 19 | + { |
| 20 | + $level = strtoupper($record->level->name); |
| 21 | + // Make sure everything is kept on one line to count as one record |
| 22 | + $message = str_replace(["\r\n", "\r", "\n"], ' ', $record->message); |
| 23 | + $json = $this->toJson($this->normalizeRecord($record), true); |
| 24 | + |
| 25 | + return "$level\t$message\t$json\n"; |
| 26 | + } |
| 27 | + |
| 28 | + public function formatBatch(array $records): string |
| 29 | + { |
| 30 | + return implode('', array_map(fn (LogRecord $record) => $this->format($record), $records)); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * @return array<array|bool|float|int|\stdClass|string|null> |
| 35 | + */ |
| 36 | + protected function normalizeRecord(LogRecord $record): array |
| 37 | + { |
| 38 | + $data = [ |
| 39 | + 'message' => $record->message, |
| 40 | + 'level' => strtoupper($record->level->name), |
| 41 | + ]; |
| 42 | + $context = $record->context; |
| 43 | + // Move any exception to the root |
| 44 | + $exception = $context['exception'] ?? null; |
| 45 | + if ($exception instanceof Throwable) { |
| 46 | + $data['exception'] = $exception; |
| 47 | + unset($context['exception']); |
| 48 | + } |
| 49 | + if ($context !== []) { |
| 50 | + $data['context'] = $context; |
| 51 | + } |
| 52 | + if ($record->extra !== []) { |
| 53 | + $data['extra'] = $record->extra; |
| 54 | + } |
| 55 | + |
| 56 | + return $this->normalize($data); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * @return scalar|array<array|scalar|object|null>|object|null |
| 61 | + */ |
| 62 | + protected function normalize(mixed $data, int $depth = 0): mixed |
| 63 | + { |
| 64 | + if ($depth > $this->maxNormalizeDepth) { |
| 65 | + return 'Over ' . $this->maxNormalizeDepth . ' levels deep, aborting normalization'; |
| 66 | + } |
| 67 | + |
| 68 | + if (is_array($data)) { |
| 69 | + $normalized = []; |
| 70 | + |
| 71 | + $count = 1; |
| 72 | + foreach ($data as $key => $value) { |
| 73 | + if ($count++ > $this->maxNormalizeItemCount) { |
| 74 | + $normalized['...'] = 'Over ' . $this->maxNormalizeItemCount . ' items (' . count($data) . ' total), aborting normalization'; |
| 75 | + break; |
| 76 | + } |
| 77 | + |
| 78 | + $normalized[$key] = $this->normalize($value, $depth + 1); |
| 79 | + } |
| 80 | + |
| 81 | + return $normalized; |
| 82 | + } |
| 83 | + |
| 84 | + if (is_object($data)) { |
| 85 | + if ($data instanceof DateTimeInterface) { |
| 86 | + return $this->formatDate($data); |
| 87 | + } |
| 88 | + |
| 89 | + if ($data instanceof Throwable) { |
| 90 | + return $this->normalizeException($data, $depth); |
| 91 | + } |
| 92 | + |
| 93 | + // if the object has specific json serializability we want to make sure we skip the __toString treatment below |
| 94 | + if ($data instanceof JsonSerializable) { |
| 95 | + return $data; |
| 96 | + } |
| 97 | + |
| 98 | + if ($data instanceof Stringable) { |
| 99 | + return $data->__toString(); |
| 100 | + } |
| 101 | + |
| 102 | + if (get_class($data) === '__PHP_Incomplete_Class') { |
| 103 | + return new ArrayObject($data); |
| 104 | + } |
| 105 | + |
| 106 | + return $data; |
| 107 | + } |
| 108 | + |
| 109 | + if (is_resource($data)) { |
| 110 | + return parent::normalize($data); |
| 111 | + } |
| 112 | + |
| 113 | + return $data; |
| 114 | + } |
| 115 | +} |
0 commit comments