From cb80ec7ebbbc76e6ffe1faa5cdd86280bd47859d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Thu, 4 Dec 2025 12:03:31 +0100 Subject: [PATCH] feat(log): Add script name and occ command to log details MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This will help when troubleshooting issues. For web request we have method and url, but for cron and occ currently we have no way to know if it’s one or the other and which command. Signed-off-by: Côme Chilliet --- build/psalm-baseline.xml | 5 ----- lib/private/AppFramework/Http/Request.php | 2 +- lib/private/Log/LogDetails.php | 21 ++++++++++++++++----- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/build/psalm-baseline.xml b/build/psalm-baseline.xml index 8fbc32c4fec..a84d9e0283a 100644 --- a/build/psalm-baseline.xml +++ b/build/psalm-baseline.xml @@ -3951,11 +3951,6 @@ - - - getMethod())]]> - - server['SCRIPT_NAME']; + $name = $this->server['SCRIPT_NAME'] ?? ''; $overwriteWebRoot = $this->config->getSystemValueString('overwritewebroot'); if ($overwriteWebRoot !== '' && $this->isOverwriteCondition()) { // FIXME: This code is untestable due to __DIR__, also that hardcoded path is really dangerous diff --git a/lib/private/Log/LogDetails.php b/lib/private/Log/LogDetails.php index 6063b25cef9..21abb6d85cb 100644 --- a/lib/private/Log/LogDetails.php +++ b/lib/private/Log/LogDetails.php @@ -1,12 +1,17 @@ config->getValue('logdateformat', \DateTimeInterface::ATOM); $logTimeZone = $this->config->getValue('logtimezone', 'UTC'); @@ -30,13 +35,13 @@ abstract class LogDetails { // apply timezone if $time is created from UNIX timestamp $time->setTimezone($timezone); } - $request = \OC::$server->getRequest(); + $request = Server::get(IRequest::class); $reqId = $request->getId(); $remoteAddr = $request->getRemoteAddress(); // remove username/passwords from URLs before writing the to the log file $time = $time->format($format); $url = ($request->getRequestUri() !== '') ? $request->getRequestUri() : '--'; - $method = is_string($request->getMethod()) ? $request->getMethod() : '--'; + $method = $request->getMethod(); if ($this->config->getValue('installed', false)) { $user = \OC_User::getUser() ?: '--'; } else { @@ -47,6 +52,7 @@ abstract class LogDetails { $userAgent = '--'; } $version = $this->config->getValue('version', ''); + $scriptName = $request->getScriptName(); $entry = compact( 'reqId', 'level', @@ -56,14 +62,19 @@ abstract class LogDetails { 'app', 'method', 'url', + 'scriptName', 'message', 'userAgent', - 'version' + 'version', ); $clientReqId = $request->getHeader('X-Request-Id'); if ($clientReqId !== '') { $entry['clientReqId'] = $clientReqId; } + if (\OC::$CLI) { + /* Only logging the command, not the parameters */ + $entry['occ_command'] = array_slice($_SERVER['argv'] ?? [], 0, 2); + } if (is_array($message)) { // Exception messages are extracted and the exception is put into a separate field @@ -82,7 +93,7 @@ abstract class LogDetails { return $entry; } - public function logDetailsAsJSON(string $app, $message, int $level): string { + public function logDetailsAsJSON(string $app, string|array $message, int $level): string { $entry = $this->logDetails($app, $message, $level); // PHP's json_encode only accept proper UTF-8 strings, loop over all // elements to ensure that they are properly UTF-8 compliant or convert