Merge pull request #56843 from nextcloud/feat/add-cli-details-to-log

feat(log): Add script name and occ command to log details
pull/56790/head
Côme Chilliet 2025-12-04 17:06:00 +07:00 committed by GitHub
commit aeed32cfba
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 17 additions and 11 deletions

@ -3951,11 +3951,6 @@
<code><![CDATA[$limit === null]]></code> <code><![CDATA[$limit === null]]></code>
</TypeDoesNotContainNull> </TypeDoesNotContainNull>
</file> </file>
<file src="lib/private/Log/LogDetails.php">
<RedundantCondition>
<code><![CDATA[is_string($request->getMethod())]]></code>
</RedundantCondition>
</file>
<file src="lib/private/Log/Systemdlog.php"> <file src="lib/private/Log/Systemdlog.php">
<UndefinedFunction> <UndefinedFunction>
<code><![CDATA[sd_journal_send('PRIORITY=' . $journal_level, <code><![CDATA[sd_journal_send('PRIORITY=' . $journal_level,

@ -769,7 +769,7 @@ class Request implements \ArrayAccess, \Countable, IRequest {
* @return string the script name * @return string the script name
*/ */
public function getScriptName(): string { public function getScriptName(): string {
$name = $this->server['SCRIPT_NAME']; $name = $this->server['SCRIPT_NAME'] ?? '';
$overwriteWebRoot = $this->config->getSystemValueString('overwritewebroot'); $overwriteWebRoot = $this->config->getSystemValueString('overwritewebroot');
if ($overwriteWebRoot !== '' && $this->isOverwriteCondition()) { if ($overwriteWebRoot !== '' && $this->isOverwriteCondition()) {
// FIXME: This code is untestable due to __DIR__, also that hardcoded path is really dangerous // FIXME: This code is untestable due to __DIR__, also that hardcoded path is really dangerous

@ -1,12 +1,17 @@
<?php <?php
declare(strict_types=1);
/** /**
* SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later * SPDX-License-Identifier: AGPL-3.0-or-later
*/ */
namespace OC\Log; namespace OC\Log;
use OC\SystemConfig; use OC\SystemConfig;
use OCP\IRequest;
use OCP\Server;
abstract class LogDetails { abstract class LogDetails {
public function __construct( public function __construct(
@ -14,7 +19,7 @@ abstract class LogDetails {
) { ) {
} }
public function logDetails(string $app, $message, int $level): array { public function logDetails(string $app, string|array $message, int $level): array {
// default to ISO8601 // default to ISO8601
$format = $this->config->getValue('logdateformat', \DateTimeInterface::ATOM); $format = $this->config->getValue('logdateformat', \DateTimeInterface::ATOM);
$logTimeZone = $this->config->getValue('logtimezone', 'UTC'); $logTimeZone = $this->config->getValue('logtimezone', 'UTC');
@ -30,13 +35,13 @@ abstract class LogDetails {
// apply timezone if $time is created from UNIX timestamp // apply timezone if $time is created from UNIX timestamp
$time->setTimezone($timezone); $time->setTimezone($timezone);
} }
$request = \OC::$server->getRequest(); $request = Server::get(IRequest::class);
$reqId = $request->getId(); $reqId = $request->getId();
$remoteAddr = $request->getRemoteAddress(); $remoteAddr = $request->getRemoteAddress();
// remove username/passwords from URLs before writing the to the log file // remove username/passwords from URLs before writing the to the log file
$time = $time->format($format); $time = $time->format($format);
$url = ($request->getRequestUri() !== '') ? $request->getRequestUri() : '--'; $url = ($request->getRequestUri() !== '') ? $request->getRequestUri() : '--';
$method = is_string($request->getMethod()) ? $request->getMethod() : '--'; $method = $request->getMethod();
if ($this->config->getValue('installed', false)) { if ($this->config->getValue('installed', false)) {
$user = \OC_User::getUser() ?: '--'; $user = \OC_User::getUser() ?: '--';
} else { } else {
@ -47,6 +52,7 @@ abstract class LogDetails {
$userAgent = '--'; $userAgent = '--';
} }
$version = $this->config->getValue('version', ''); $version = $this->config->getValue('version', '');
$scriptName = $request->getScriptName();
$entry = compact( $entry = compact(
'reqId', 'reqId',
'level', 'level',
@ -56,14 +62,19 @@ abstract class LogDetails {
'app', 'app',
'method', 'method',
'url', 'url',
'scriptName',
'message', 'message',
'userAgent', 'userAgent',
'version' 'version',
); );
$clientReqId = $request->getHeader('X-Request-Id'); $clientReqId = $request->getHeader('X-Request-Id');
if ($clientReqId !== '') { if ($clientReqId !== '') {
$entry['clientReqId'] = $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)) { if (is_array($message)) {
// Exception messages are extracted and the exception is put into a separate field // Exception messages are extracted and the exception is put into a separate field
@ -82,7 +93,7 @@ abstract class LogDetails {
return $entry; 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); $entry = $this->logDetails($app, $message, $level);
// PHP's json_encode only accept proper UTF-8 strings, loop over all // 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 // elements to ensure that they are properly UTF-8 compliant or convert