Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions build/psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2197,11 +2197,6 @@
<code><![CDATA[$limit === null]]></code>
</TypeDoesNotContainNull>
</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">
<UndefinedFunction>
<code><![CDATA[sd_journal_send('PRIORITY=' . $journal_level,
Expand Down
2 changes: 1 addition & 1 deletion lib/private/AppFramework/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ public function getPathInfo() {
* @return string the script name
*/
public function getScriptName(): string {
$name = $this->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
Expand Down
22 changes: 17 additions & 5 deletions lib/private/Log/LogDetails.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OC\Log;

use OC\SystemConfig;
use OCP\IRequest;
use OCP\Server;

abstract class LogDetails {
public function __construct(
private SystemConfig $config,
) {
}

public function logDetails(string $app, $message, int $level): array {
public function logDetails(string $app, string|array $message, int $level): array {
// default to ISO8601
$format = $this->config->getValue('logdateformat', \DateTimeInterface::ATOM);
$logTimeZone = $this->config->getValue('logtimezone', 'UTC');
Expand All @@ -29,13 +35,13 @@ public function logDetails(string $app, $message, int $level): array {
// 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 {
Expand All @@ -46,6 +52,7 @@ public function logDetails(string $app, $message, int $level): array {
$userAgent = '--';
}
$version = $this->config->getValue('version', '');
$scriptName = $request->getScriptName();
$entry = compact(
'reqId',
'level',
Expand All @@ -55,14 +62,19 @@ public function logDetails(string $app, $message, int $level): array {
'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
Expand All @@ -81,7 +93,7 @@ public function logDetails(string $app, $message, int $level): array {
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
Expand Down
Loading