Skip to content

Commit eb62e97

Browse files
committed
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 e5367e1 commit eb62e97

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

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)