Skip to content

Commit 13c33d9

Browse files
authored
Merge pull request #56852 from nextcloud/backport/56843/stable31
[stable31] feat(log): Add script name and occ command to log details
2 parents 83bb7bc + 37b105e commit 13c33d9

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

build/psalm-baseline.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2197,11 +2197,6 @@
21972197
<code><![CDATA[$limit === null]]></code>
21982198
</TypeDoesNotContainNull>
21992199
</file>
2200-
<file src="lib/private/Log/LogDetails.php">
2201-
<RedundantCondition>
2202-
<code><![CDATA[is_string($request->getMethod())]]></code>
2203-
</RedundantCondition>
2204-
</file>
22052200
<file src="lib/private/Log/Systemdlog.php">
22062201
<UndefinedFunction>
22072202
<code><![CDATA[sd_journal_send('PRIORITY=' . $journal_level,

lib/private/AppFramework/Http/Request.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,7 @@ public function getPathInfo() {
744744
* @return string the script name
745745
*/
746746
public function getScriptName(): string {
747-
$name = $this->server['SCRIPT_NAME'];
747+
$name = $this->server['SCRIPT_NAME'] ?? '';
748748
$overwriteWebRoot = $this->config->getSystemValueString('overwritewebroot');
749749
if ($overwriteWebRoot !== '' && $this->isOverwriteCondition()) {
750750
// FIXME: This code is untestable due to __DIR__, also that hardcoded path is really dangerous

lib/private/Log/LogDetails.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
<?php
2+
3+
declare(strict_types=1);
4+
25
/**
36
* SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
47
* SPDX-License-Identifier: AGPL-3.0-or-later
58
*/
9+
610
namespace OC\Log;
711

812
use OC\SystemConfig;
13+
use OCP\IRequest;
14+
use OCP\Server;
915

1016
abstract class LogDetails {
1117
public function __construct(
1218
private SystemConfig $config,
1319
) {
1420
}
1521

16-
public function logDetails(string $app, $message, int $level): array {
22+
public function logDetails(string $app, string|array $message, int $level): array {
1723
// default to ISO8601
1824
$format = $this->config->getValue('logdateformat', \DateTimeInterface::ATOM);
1925
$logTimeZone = $this->config->getValue('logtimezone', 'UTC');
@@ -29,13 +35,13 @@ public function logDetails(string $app, $message, int $level): array {
2935
// apply timezone if $time is created from UNIX timestamp
3036
$time->setTimezone($timezone);
3137
}
32-
$request = \OC::$server->getRequest();
38+
$request = Server::get(IRequest::class);
3339
$reqId = $request->getId();
3440
$remoteAddr = $request->getRemoteAddress();
3541
// remove username/passwords from URLs before writing the to the log file
3642
$time = $time->format($format);
3743
$url = ($request->getRequestUri() !== '') ? $request->getRequestUri() : '--';
38-
$method = is_string($request->getMethod()) ? $request->getMethod() : '--';
44+
$method = $request->getMethod();
3945
if ($this->config->getValue('installed', false)) {
4046
$user = \OC_User::getUser() ?: '--';
4147
} else {
@@ -46,6 +52,7 @@ public function logDetails(string $app, $message, int $level): array {
4652
$userAgent = '--';
4753
}
4854
$version = $this->config->getValue('version', '');
55+
$scriptName = $request->getScriptName();
4956
$entry = compact(
5057
'reqId',
5158
'level',
@@ -55,14 +62,19 @@ public function logDetails(string $app, $message, int $level): array {
5562
'app',
5663
'method',
5764
'url',
65+
'scriptName',
5866
'message',
5967
'userAgent',
60-
'version'
68+
'version',
6169
);
6270
$clientReqId = $request->getHeader('X-Request-Id');
6371
if ($clientReqId !== '') {
6472
$entry['clientReqId'] = $clientReqId;
6573
}
74+
if (\OC::$CLI) {
75+
/* Only logging the command, not the parameters */
76+
$entry['occ_command'] = array_slice($_SERVER['argv'] ?? [], 0, 2);
77+
}
6678

6779
if (is_array($message)) {
6880
// Exception messages are extracted and the exception is put into a separate field
@@ -81,7 +93,7 @@ public function logDetails(string $app, $message, int $level): array {
8193
return $entry;
8294
}
8395

84-
public function logDetailsAsJSON(string $app, $message, int $level): string {
96+
public function logDetailsAsJSON(string $app, string|array $message, int $level): string {
8597
$entry = $this->logDetails($app, $message, $level);
8698
// PHP's json_encode only accept proper UTF-8 strings, loop over all
8799
// elements to ensure that they are properly UTF-8 compliant or convert

0 commit comments

Comments
 (0)