Lines
100.00%
16 / 16
Functions and Methods
100.00%
2 / 2
Classes and Traits
100.00%
1 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| JsonLogFormatter | 100.00% | 16 / 16 | 100.00% | 2 / 2 | 6 | 100.00% | 1 / 1 | |||
| format | 100.00% | 15 / 15 | 100.00% | 1 / 1 | 4 | |||||
| emptyAsObject | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 2 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace BO\Slim\Formatter; | |
| 6 | ||
| 7 | use BO\Slim\Bootstrap; | |
| 8 | use Monolog\Formatter\JsonFormatter; | |
| 9 | use Monolog\LogRecord; | |
| 10 | ||
| 11 | /** | |
| 12 | * JSON log line used by CAP/Kubernetes collectors. | |
| 13 | * Monolog 3 processors must return a LogRecord, so the custom field list lives here. | |
| 14 | */ | |
| 15 | class JsonLogFormatter extends JsonFormatter | |
| 16 | { | |
| 17 | #[\Override] | |
| 18 | public function format(LogRecord $record): string | |
| 19 | { | |
| 20 | $normalized = [ | |
| 21 | 'time_local' => (new \DateTime())->format('Y-m-d\TH:i:sP'), | |
| 22 | 'client_ip' => $_SERVER['REMOTE_ADDR'] ?? '', | |
| 23 | 'remote_addr' => $_SERVER['HTTP_X_FORWARDED_FOR'] ?? $_SERVER['REMOTE_ADDR'] ?? '', | |
| 24 | 'remote_user' => '', | |
| 25 | 'application' => defined('\\App::IDENTIFIER') ? \App::IDENTIFIER : 'zms', | |
| 26 | 'module' => defined('\\App::MODULE_NAME') ? \App::MODULE_NAME : 'zmsslim', | |
| 27 | 'cron' => Bootstrap::isCronLogging(), | |
| 28 | 'cron_name' => Bootstrap::getCronLogName(), | |
| 29 | 'message' => $record->message, | |
| 30 | 'level' => $record->level->getName(), | |
| 31 | 'context' => $this->emptyAsObject($this->normalize($record->context)), | |
| 32 | 'extra' => $this->emptyAsObject($this->normalize($record->extra)), | |
| 33 | ]; | |
| 34 | ||
| 35 | return $this->toJson($normalized, true) . ($this->isAppendingNewlines() ? "\n" : ''); | |
| 36 | } | |
| 37 | ||
| 38 | private function emptyAsObject(mixed $value): mixed | |
| 39 | { | |
| 40 | return $value === [] ? new \stdClass() : $value; | |
| 41 | } | |
| 42 | } |