|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Gotphoto\Logging; |
| 6 | + |
| 7 | +use Monolog\Formatter\NormalizerFormatter; |
| 8 | +use Monolog\LogRecord; |
| 9 | + |
| 10 | +final class LogstashFormatter extends ExceptionNormalizerFormatter |
| 11 | +{ |
| 12 | + /** |
| 13 | + * @var string the name of the system for the Logstash log message, used to fill the @source field |
| 14 | + */ |
| 15 | + protected string $systemName; |
| 16 | + |
| 17 | + /** |
| 18 | + * @param string $applicationName the application that sends the data, used as the "type" |
| 19 | + * field of logstash |
| 20 | + * @param string $environment current environment |
| 21 | + * @param string|null $systemName the system/machine name, used as the "source" field of |
| 22 | + * logstash, defaults to the hostname of the machine |
| 23 | + * @param array<string, array<callable(\Throwable):array<string, array<array-key, string>|int|string>>> $exceptionContextProviderMap |
| 24 | + */ |
| 25 | + public function __construct( |
| 26 | + protected string $applicationName, |
| 27 | + protected string $environment, |
| 28 | + protected array $exceptionContextProviderMap = [], |
| 29 | + ?string $systemName = null, |
| 30 | + ) { |
| 31 | + // logstash requires a ISO 8601 format date with optional millisecond precision. |
| 32 | + parent::__construct($exceptionContextProviderMap, 'Y-m-d\TH:i:s.uP'); |
| 33 | + $this->systemName = $systemName === null ? gethostname() : $systemName; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * {@inheritdoc} |
| 38 | + */ |
| 39 | + public function format(LogRecord $record): string |
| 40 | + { |
| 41 | + /** @var array{timestamp: int, datetime: string, extra?:array, context?:array} $data */ |
| 42 | + $data = parent::format($record); |
| 43 | + /** @psalm-suppress RiskyTruthyFalsyComparison this is okay null or empty string */ |
| 44 | + if (empty($data['datetime'])) { |
| 45 | + $data['datetime'] = gmdate('c'); |
| 46 | + } |
| 47 | + $message = [ |
| 48 | + '@timestamp' => $data['datetime'], |
| 49 | + '@version' => 1, |
| 50 | + 'timestamp' => $data['timestamp'], |
| 51 | + 'host' => $this->systemName, |
| 52 | + 'environment' => $this->environment, |
| 53 | + ]; |
| 54 | + unset($data['datetime'], $data['timestamp']); |
| 55 | + if ($this->applicationName) { |
| 56 | + $message['app'] = $this->applicationName; |
| 57 | + } |
| 58 | + $message += $data; |
| 59 | + if (isset($message['extra'])) { |
| 60 | + $message['extra'] = (object)$message['extra']; |
| 61 | + } |
| 62 | + if (isset($message['context'])) { |
| 63 | + $message['context'] = (object)$message['context']; |
| 64 | + } |
| 65 | + |
| 66 | + return $this->toJson($message) . "\n"; |
| 67 | + } |
| 68 | + |
| 69 | + public function formatBatch(array $records) |
| 70 | + { |
| 71 | + /** @var array $records */ |
| 72 | + $records = parent::formatBatch($records); |
| 73 | + |
| 74 | + return $this->toJson($records, true); |
| 75 | + } |
| 76 | +} |
0 commit comments