diff --git a/lib/private/Log/LogFactory.php b/lib/private/Log/LogFactory.php index ee6054b81f8fd..84d7411c4f965 100644 --- a/lib/private/Log/LogFactory.php +++ b/lib/private/Log/LogFactory.php @@ -1,5 +1,4 @@ new Errorlog($this->systemConfig), + 'stdlog' => new Stdlog($this->systemConfig), 'syslog' => $this->c->resolve(Syslog::class), 'systemd' => $this->c->resolve(Systemdlog::class), 'file' => $this->buildLogFile(), @@ -36,6 +36,7 @@ public function get(string $type):IWriter { protected function createNewLogger(string $type, string $tag, string $path): IWriter { return match (strtolower($type)) { 'errorlog' => new Errorlog($this->systemConfig, $tag), + 'stdlog' => new Stdlog($this->systemConfig, $tag), 'syslog' => new Syslog($this->systemConfig, $tag), 'systemd' => new Systemdlog($this->systemConfig, $tag), default => $this->buildLogFile($path), diff --git a/lib/private/Log/Stdlog.php b/lib/private/Log/Stdlog.php new file mode 100644 index 0000000000000..65493d753a950 --- /dev/null +++ b/lib/private/Log/Stdlog.php @@ -0,0 +1,76 @@ +logDetailsAsJSON($app, $message, $level); + $details = json_decode($detailsJson, true); + + if (json_last_error() !== JSON_ERROR_NONE || !is_array($details)) { + return; + } + + $logEntry = array_merge([ + 'tag' => $this->tag, + 'app' => $app, + 'level' => $level, + ], $details); + $traceparent = $_SERVER['HTTP_TRACEPARENT']; + if (preg_match('/^00-([0-9a-f]{32})-([0-9a-f]{16})-([0-9a-f]{2})$/', $traceparent, $matches)) { + $gcp = getenv('GOOGLE_CLOUD_PROJECT'); + if (!empty($gcp)) { + $logEntry['logging.googleapis.com/trace'] = 'projects/' . $gcp . '/traces/' . $matches[1]; + $logEntry['logging.googleapis.com/spanId'] = $matches[2]; + } else { + $logEntry['traceId'] = $matches[1]; + $logEntry['spanId'] = $matches[2]; + } + } + // Check if 'message' field exists and is a string + if (isset($logEntry['message']) && is_string($logEntry['message'])) { + $msg = $logEntry['message']; + + if (strlen($msg) > 0 && $msg[0] === '{') { + // Try decoding JSON + $decoded = json_decode($msg, true); + + if (json_last_error() === JSON_ERROR_NONE && is_array($decoded)) { + // Remove original 'message' field + unset($logEntry['message']); + + // Flatten decoded JSON into top-level logEntry + // This will overwrite existing keys if there are + // conflicts + $logEntry = array_merge($logEntry, $decoded); + } + } + } + + $json = json_encode($logEntry, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); + + if ($json !== false) { + file_put_contents('php://stderr', $json . PHP_EOL); + } + } +}