Lines 100.00% 16 / 16
Methods 100.00% 2 / 2
Classes 100.00% 1 / 1
Name Lines Methods CRAP
 format 100.00% 15 / 15 100.00% 1 / 1 4
 emptyAsObject 100.00% 1 / 1 100.00% 1 / 1 2
15class 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}