Skip to content

Commit ba8514c

Browse files
authored
Merge pull request #56851 from nextcloud/backport/56843/stable32
[stable32] feat(log): Add script name and occ command to log details
2 parents e0e80c8 + dcdb536 commit ba8514c

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
@@ -4050,11 +4050,6 @@
40504050
<code><![CDATA[$limit === null]]></code>
40514051
</TypeDoesNotContainNull>
40524052
</file>
4053-
<file src="lib/private/Log/LogDetails.php">
4054-
<RedundantCondition>
4055-
<code><![CDATA[is_string($request->getMethod())]]></code>
4056-
</RedundantCondition>
4057-
</file>
40584053
<file src="lib/private/Log/Systemdlog.php">
40594054
<UndefinedFunction>
40604055
<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)