Skip to content

Commit 20ced3d

Browse files
come-ncbackportbot[bot]
authored andcommitted
feat(log): Add script name and occ command to log details
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 <[email protected]>
1 parent 36bb676 commit 20ced3d

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

build/psalm-baseline.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4055,11 +4055,6 @@
40554055
<code><![CDATA[$limit === null]]></code>
40564056
</TypeDoesNotContainNull>
40574057
</file>
4058-
<file src="lib/private/Log/LogDetails.php">
4059-
<RedundantCondition>
4060-
<code><![CDATA[is_string($request->getMethod())]]></code>
4061-
</RedundantCondition>
4062-
</file>
40634058
<file src="lib/private/Log/Systemdlog.php">
40644059
<UndefinedFunction>
40654060
<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
@@ -769,7 +769,7 @@ public function getPathInfo(): string|false {
769769
* @return string the script name
770770
*/
771771
public function getScriptName(): string {
772-
$name = $this->server['SCRIPT_NAME'];
772+
$name = $this->server['SCRIPT_NAME'] ?? '';
773773
$overwriteWebRoot = $this->config->getSystemValueString('overwritewebroot');
774774
if ($overwriteWebRoot !== '' && $this->isOverwriteCondition()) {
775775
// FIXME: This code is untestable due to __DIR__, also that hardcoded path is really dangerous

lib/private/Log/LogDetails.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
/**
46
* SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
57
* SPDX-License-Identifier: AGPL-3.0-or-later
68
*/
9+
710
namespace OC\Log;
811

912
use OC\SystemConfig;
13+
use OCP\IRequest;
14+
use OCP\Server;
1015

1116
abstract class LogDetails {
1217
public function __construct(
1318
private SystemConfig $config,
1419
) {
1520
}
1621

17-
public function logDetails(string $app, $message, int $level): array {
22+
public function logDetails(string $app, string|array $message, int $level): array {
1823
// default to ISO8601
1924
$format = $this->config->getValue('logdateformat', \DateTimeInterface::ATOM);
2025
$logTimeZone = $this->config->getValue('logtimezone', 'UTC');
@@ -30,13 +35,13 @@ public function logDetails(string $app, $message, int $level): array {
3035
// apply timezone if $time is created from UNIX timestamp
3136
$time->setTimezone($timezone);
3237
}
33-
$request = \OC::$server->getRequest();
38+
$request = Server::get(IRequest::class);
3439
$reqId = $request->getId();
3540
$remoteAddr = $request->getRemoteAddress();
3641
// remove username/passwords from URLs before writing the to the log file
3742
$time = $time->format($format);
3843
$url = ($request->getRequestUri() !== '') ? $request->getRequestUri() : '--';
39-
$method = is_string($request->getMethod()) ? $request->getMethod() : '--';
44+
$method = $request->getMethod();
4045
if ($this->config->getValue('installed', false)) {
4146
$user = \OC_User::getUser() ?: '--';
4247
} else {
@@ -47,6 +52,7 @@ public function logDetails(string $app, $message, int $level): array {
4752
$userAgent = '--';
4853
}
4954
$version = $this->config->getValue('version', '');
55+
$scriptName = $request->getScriptName();
5056
$entry = compact(
5157
'reqId',
5258
'level',
@@ -56,14 +62,19 @@ public function logDetails(string $app, $message, int $level): array {
5662
'app',
5763
'method',
5864
'url',
65+
'scriptName',
5966
'message',
6067
'userAgent',
61-
'version'
68+
'version',
6269
);
6370
$clientReqId = $request->getHeader('X-Request-Id');
6471
if ($clientReqId !== '') {
6572
$entry['clientReqId'] = $clientReqId;
6673
}
74+
if (\OC::$CLI) {
75+
/* Only logging the command, not the parameters */
76+
$entry['occ_command'] = array_slice($_SERVER['argv'] ?? [], 0, 2);
77+
}
6778

6879
if (is_array($message)) {
6980
// Exception messages are extracted and the exception is put into a separate field
@@ -82,7 +93,7 @@ public function logDetails(string $app, $message, int $level): array {
8293
return $entry;
8394
}
8495

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

0 commit comments

Comments
 (0)