diff options
Diffstat (limited to 'lib/private/Log')
-rw-r--r-- | lib/private/Log/ErrorHandler.php | 4 | ||||
-rw-r--r-- | lib/private/Log/Errorlog.php | 2 | ||||
-rw-r--r-- | lib/private/Log/ExceptionSerializer.php | 48 | ||||
-rw-r--r-- | lib/private/Log/File.php | 8 | ||||
-rw-r--r-- | lib/private/Log/LogDetails.php | 9 | ||||
-rw-r--r-- | lib/private/Log/LogFactory.php | 9 | ||||
-rw-r--r-- | lib/private/Log/PsrLoggerAdapter.php | 137 | ||||
-rw-r--r-- | lib/private/Log/Rotate.php | 12 | ||||
-rw-r--r-- | lib/private/Log/Syslog.php | 8 | ||||
-rw-r--r-- | lib/private/Log/Systemdlog.php | 5 |
10 files changed, 110 insertions, 132 deletions
diff --git a/lib/private/Log/ErrorHandler.php b/lib/private/Log/ErrorHandler.php index e1faf336118..6597274a868 100644 --- a/lib/private/Log/ErrorHandler.php +++ b/lib/private/Log/ErrorHandler.php @@ -72,9 +72,9 @@ class ErrorHandler { private static function errnoToLogLevel(int $errno): int { return match ($errno) { - E_USER_WARNING => ILogger::WARN, + E_WARNING, E_USER_WARNING => ILogger::WARN, E_DEPRECATED, E_USER_DEPRECATED => ILogger::DEBUG, - E_USER_NOTICE => ILogger::INFO, + E_NOTICE, E_USER_NOTICE => ILogger::INFO, default => ILogger::ERROR, }; } diff --git a/lib/private/Log/Errorlog.php b/lib/private/Log/Errorlog.php index d53b8b151a1..6188bb70fd5 100644 --- a/lib/private/Log/Errorlog.php +++ b/lib/private/Log/Errorlog.php @@ -26,6 +26,6 @@ class Errorlog extends LogDetails implements IWriter { * @param string|array $message */ public function write(string $app, $message, int $level): void { - error_log('[' . $this->tag . ']['.$app.']['.$level.'] '.$this->logDetailsAsJSON($app, $message, $level)); + error_log('[' . $this->tag . '][' . $app . '][' . $level . '] ' . $this->logDetailsAsJSON($app, $message, $level)); } } diff --git a/lib/private/Log/ExceptionSerializer.php b/lib/private/Log/ExceptionSerializer.php index 03beed7cce1..af7c9e48435 100644 --- a/lib/private/Log/ExceptionSerializer.php +++ b/lib/private/Log/ExceptionSerializer.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later @@ -6,6 +7,7 @@ namespace OC\Log; use OC\Core\Controller\SetupController; +use OC\Http\Client\Client; use OC\Security\IdentityProof\Key; use OC\Setup; use OC\SystemConfig; @@ -13,8 +15,9 @@ use OCA\Encryption\Controller\RecoveryController; use OCA\Encryption\Controller\SettingsController; use OCA\Encryption\Crypto\Crypt; use OCA\Encryption\Crypto\Encryption; -use OCA\Encryption\Hooks\UserHooks; use OCA\Encryption\KeyManager; +use OCA\Encryption\Listeners\UserEventsListener; +use OCA\Encryption\Services\PassphraseService; use OCA\Encryption\Session; use OCP\HintException; @@ -32,6 +35,7 @@ class ExceptionSerializer { 'validateUserPass', 'loginWithToken', '{closure}', + '{closure:*', 'createSessionToken', // Provisioning @@ -106,6 +110,22 @@ class ExceptionSerializer { Key::class => [ '__construct' ], + Client::class => [ + 'request', + 'delete', + 'deleteAsync', + 'get', + 'getAsync', + 'head', + 'headAsync', + 'options', + 'optionsAsync', + 'patch', + 'post', + 'postAsync', + 'put', + 'putAsync', + ], \Redis::class => [ 'auth' ], @@ -152,14 +172,16 @@ class ExceptionSerializer { \OCA\Encryption\Users\Setup::class => [ 'setupUser', ], - UserHooks::class => [ - 'login', - 'postCreateUser', - 'postDeleteUser', - 'prePasswordReset', - 'postPasswordReset', - 'preSetPassphrase', - 'setPassphrase', + UserEventsListener::class => [ + 'handle', + 'onUserCreated', + 'onUserLogin', + 'onBeforePasswordUpdated', + 'onPasswordUpdated', + 'onPasswordReset', + ], + PassphraseService::class => [ + 'setPassphraseForUser', ], ]; @@ -180,7 +202,9 @@ class ExceptionSerializer { return $this->editTrace($sensitiveValues, $traceLine); } foreach (self::methodsWithSensitiveParameters as $sensitiveMethod) { - if (str_contains($traceLine['function'], $sensitiveMethod)) { + if (str_contains($traceLine['function'], $sensitiveMethod) + || (str_ends_with($sensitiveMethod, '*') + && str_starts_with($traceLine['function'], substr($sensitiveMethod, 0, -1)))) { return $this->editTrace($sensitiveValues, $traceLine); } } @@ -196,13 +220,13 @@ class ExceptionSerializer { private function removeValuesFromArgs($args, $values): array { $workArgs = []; - foreach ($args as $arg) { + foreach ($args as $key => $arg) { if (in_array($arg, $values, true)) { $arg = self::SENSITIVE_VALUE_PLACEHOLDER; } elseif (is_array($arg)) { $arg = $this->removeValuesFromArgs($arg, $values); } - $workArgs[] = $arg; + $workArgs[$key] = $arg; } return $workArgs; } diff --git a/lib/private/Log/File.php b/lib/private/Log/File.php index 28cc856b980..ba428ba185b 100644 --- a/lib/private/Log/File.php +++ b/lib/private/Log/File.php @@ -55,7 +55,7 @@ class File extends LogDetails implements IWriter, IFileBased { @chmod($this->logFile, $this->logFileMode); } if ($handle) { - fwrite($handle, $entry."\n"); + fwrite($handle, $entry . "\n"); fclose($handle); } else { // Fall back to error_log @@ -73,7 +73,7 @@ class File extends LogDetails implements IWriter, IFileBased { * get entries from the log in reverse chronological order */ public function getEntries(int $limit = 50, int $offset = 0): array { - $minLevel = $this->config->getValue("loglevel", ILogger::WARN); + $minLevel = $this->config->getValue('loglevel', ILogger::WARN); $entries = []; $handle = @fopen($this->logFile, 'rb'); if ($handle) { @@ -91,7 +91,7 @@ class File extends LogDetails implements IWriter, IFileBased { // Add the first character if at the start of the file, // because it doesn't hit the else in the loop if ($pos == 0) { - $line = $ch.$line; + $line = $ch . $line; } $entry = json_decode($line); // Add the line as an entry if it is passed the offset and is equal or above the log level @@ -105,7 +105,7 @@ class File extends LogDetails implements IWriter, IFileBased { $line = ''; } } else { - $line = $ch.$line; + $line = $ch . $line; } $pos--; } diff --git a/lib/private/Log/LogDetails.php b/lib/private/Log/LogDetails.php index bf2c1a22c49..6063b25cef9 100644 --- a/lib/private/Log/LogDetails.php +++ b/lib/private/Log/LogDetails.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later @@ -22,7 +23,7 @@ abstract class LogDetails { } catch (\Exception $e) { $timezone = new \DateTimeZone('UTC'); } - $time = \DateTime::createFromFormat("U.u", number_format(microtime(true), 4, ".", "")); + $time = \DateTime::createFromFormat('U.u', number_format(microtime(true), 4, '.', '')); if ($time === false) { $time = new \DateTime('now', $timezone); } else { @@ -37,7 +38,7 @@ abstract class LogDetails { $url = ($request->getRequestUri() !== '') ? $request->getRequestUri() : '--'; $method = is_string($request->getMethod()) ? $request->getMethod() : '--'; if ($this->config->getValue('installed', false)) { - $user = \OC_User::getUser() ? \OC_User::getUser() : '--'; + $user = \OC_User::getUser() ?: '--'; } else { $user = '--'; } @@ -59,6 +60,10 @@ abstract class LogDetails { 'userAgent', 'version' ); + $clientReqId = $request->getHeader('X-Request-Id'); + if ($clientReqId !== '') { + $entry['clientReqId'] = $clientReqId; + } if (is_array($message)) { // Exception messages are extracted and the exception is put into a separate field diff --git a/lib/private/Log/LogFactory.php b/lib/private/Log/LogFactory.php index a5a7290bd9c..ee6054b81f8 100644 --- a/lib/private/Log/LogFactory.php +++ b/lib/private/Log/LogFactory.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later @@ -7,7 +8,6 @@ namespace OC\Log; use OC\Log; use OC\SystemConfig; -use OCP\ILogger; use OCP\IServerContainer; use OCP\Log\ILogFactory; use OCP\Log\IWriter; @@ -33,11 +33,6 @@ class LogFactory implements ILogFactory { }; } - public function getCustomLogger(string $path): ILogger { - $log = $this->buildLogFile($path); - return new Log($log, $this->systemConfig); - } - protected function createNewLogger(string $type, string $tag, string $path): IWriter { return match (strtolower($type)) { 'errorlog' => new Errorlog($this->systemConfig, $tag), @@ -55,7 +50,7 @@ class LogFactory implements ILogFactory { } protected function buildLogFile(string $logFile = ''): File { - $defaultLogFile = $this->systemConfig->getValue('datadirectory', \OC::$SERVERROOT.'/data').'/nextcloud.log'; + $defaultLogFile = $this->systemConfig->getValue('datadirectory', \OC::$SERVERROOT . '/data') . '/nextcloud.log'; if ($logFile === '') { $logFile = $this->systemConfig->getValue('logfile', $defaultLogFile); } diff --git a/lib/private/Log/PsrLoggerAdapter.php b/lib/private/Log/PsrLoggerAdapter.php index 16e609eefdb..79e9d4d9621 100644 --- a/lib/private/Log/PsrLoggerAdapter.php +++ b/lib/private/Log/PsrLoggerAdapter.php @@ -14,6 +14,7 @@ use OCP\ILogger; use OCP\Log\IDataLogger; use Psr\Log\InvalidArgumentException; use Psr\Log\LoggerInterface; +use Psr\Log\LogLevel; use Throwable; use function array_key_exists; use function array_merge; @@ -24,6 +25,20 @@ final class PsrLoggerAdapter implements LoggerInterface, IDataLogger { ) { } + public static function logLevelToInt(string $level): int { + return match ($level) { + LogLevel::ALERT => ILogger::ERROR, + LogLevel::CRITICAL => ILogger::ERROR, + LogLevel::DEBUG => ILogger::DEBUG, + LogLevel::EMERGENCY => ILogger::FATAL, + LogLevel::ERROR => ILogger::ERROR, + LogLevel::INFO => ILogger::INFO, + LogLevel::NOTICE => ILogger::INFO, + LogLevel::WARNING => ILogger::WARN, + default => throw new InvalidArgumentException('Unsupported custom log level'), + }; + } + public function setEventDispatcher(IEventDispatcher $eventDispatcher): void { $this->logger->setEventDispatcher($eventDispatcher); } @@ -35,21 +50,11 @@ final class PsrLoggerAdapter implements LoggerInterface, IDataLogger { /** * System is unusable. * - * @param $message + * @param $message * @param mixed[] $context */ public function emergency($message, array $context = []): void { - if ($this->containsThrowable($context)) { - $this->logger->logException($context['exception'], array_merge( - [ - 'message' => (string)$message, - 'level' => ILogger::FATAL, - ], - $context - )); - } else { - $this->logger->emergency((string)$message, $context); - } + $this->log(LogLevel::EMERGENCY, (string)$message, $context); } /** @@ -58,21 +63,11 @@ final class PsrLoggerAdapter implements LoggerInterface, IDataLogger { * Example: Entire website down, database unavailable, etc. This should * trigger the SMS alerts and wake you up. * - * @param $message + * @param $message * @param mixed[] $context */ public function alert($message, array $context = []): void { - if ($this->containsThrowable($context)) { - $this->logger->logException($context['exception'], array_merge( - [ - 'message' => (string)$message, - 'level' => ILogger::ERROR, - ], - $context - )); - } else { - $this->logger->alert((string)$message, $context); - } + $this->log(LogLevel::ALERT, (string)$message, $context); } /** @@ -80,42 +75,22 @@ final class PsrLoggerAdapter implements LoggerInterface, IDataLogger { * * Example: Application component unavailable, unexpected exception. * - * @param $message + * @param $message * @param mixed[] $context */ public function critical($message, array $context = []): void { - if ($this->containsThrowable($context)) { - $this->logger->logException($context['exception'], array_merge( - [ - 'message' => (string)$message, - 'level' => ILogger::ERROR, - ], - $context - )); - } else { - $this->logger->critical((string)$message, $context); - } + $this->log(LogLevel::CRITICAL, (string)$message, $context); } /** * Runtime errors that do not require immediate action but should typically * be logged and monitored. * - * @param $message + * @param $message * @param mixed[] $context */ public function error($message, array $context = []): void { - if ($this->containsThrowable($context)) { - $this->logger->logException($context['exception'], array_merge( - [ - 'message' => (string)$message, - 'level' => ILogger::ERROR, - ], - $context - )); - } else { - $this->logger->error((string)$message, $context); - } + $this->log(LogLevel::ERROR, (string)$message, $context); } /** @@ -124,41 +99,21 @@ final class PsrLoggerAdapter implements LoggerInterface, IDataLogger { * Example: Use of deprecated APIs, poor use of an API, undesirable things * that are not necessarily wrong. * - * @param $message + * @param $message * @param mixed[] $context */ public function warning($message, array $context = []): void { - if ($this->containsThrowable($context)) { - $this->logger->logException($context['exception'], array_merge( - [ - 'message' => (string)$message, - 'level' => ILogger::WARN, - ], - $context - )); - } else { - $this->logger->warning((string)$message, $context); - } + $this->log(LogLevel::WARNING, (string)$message, $context); } /** * Normal but significant events. * - * @param $message + * @param $message * @param mixed[] $context */ public function notice($message, array $context = []): void { - if ($this->containsThrowable($context)) { - $this->logger->logException($context['exception'], array_merge( - [ - 'message' => (string)$message, - 'level' => ILogger::INFO, - ], - $context - )); - } else { - $this->logger->notice((string)$message, $context); - } + $this->log(LogLevel::NOTICE, (string)$message, $context); } /** @@ -166,55 +121,41 @@ final class PsrLoggerAdapter implements LoggerInterface, IDataLogger { * * Example: User logs in, SQL logs. * - * @param $message + * @param $message * @param mixed[] $context */ public function info($message, array $context = []): void { - if ($this->containsThrowable($context)) { - $this->logger->logException($context['exception'], array_merge( - [ - 'message' => (string)$message, - 'level' => ILogger::INFO, - ], - $context - )); - } else { - $this->logger->info((string)$message, $context); - } + $this->log(LogLevel::INFO, (string)$message, $context); } /** * Detailed debug information. * - * @param $message + * @param $message * @param mixed[] $context */ public function debug($message, array $context = []): void { - if ($this->containsThrowable($context)) { - $this->logger->logException($context['exception'], array_merge( - [ - 'message' => (string)$message, - 'level' => ILogger::DEBUG, - ], - $context - )); - } else { - $this->logger->debug((string)$message, $context); - } + $this->log(LogLevel::DEBUG, (string)$message, $context); } /** * Logs with an arbitrary level. * * @param mixed $level - * @param $message + * @param $message * @param mixed[] $context * * @throws InvalidArgumentException */ public function log($level, $message, array $context = []): void { + if (is_string($level)) { + $level = self::logLevelToInt($level); + } + if (isset($context['level']) && is_string($context['level'])) { + $context['level'] = self::logLevelToInt($context['level']); + } if (!is_int($level) || $level < ILogger::DEBUG || $level > ILogger::FATAL) { - throw new InvalidArgumentException('Nextcloud allows only integer log levels'); + throw new InvalidArgumentException('Unsupported custom log level'); } if ($this->containsThrowable($context)) { $this->logger->logException($context['exception'], array_merge( diff --git a/lib/private/Log/Rotate.php b/lib/private/Log/Rotate.php index f2fdb83623d..ee1593b87ac 100644 --- a/lib/private/Log/Rotate.php +++ b/lib/private/Log/Rotate.php @@ -7,6 +7,8 @@ */ namespace OC\Log; +use OCP\AppFramework\Utility\ITimeFactory; +use OCP\BackgroundJob\TimedJob; use OCP\IConfig; use OCP\Log\RotationTrait; use Psr\Log\LoggerInterface; @@ -17,9 +19,15 @@ use Psr\Log\LoggerInterface; * For more professional log management set the 'logfile' config to a different * location and manage that with your own tools. */ -class Rotate extends \OCP\BackgroundJob\Job { +class Rotate extends TimedJob { use RotationTrait; + public function __construct(ITimeFactory $time) { + parent::__construct($time); + + $this->setInterval(3600); + } + public function run($argument): void { $config = \OCP\Server::get(IConfig::class); $this->filePath = $config->getSystemValueString('logfile', $config->getSystemValueString('datadirectory', \OC::$SERVERROOT . '/data') . '/nextcloud.log'); @@ -27,7 +35,7 @@ class Rotate extends \OCP\BackgroundJob\Job { $this->maxSize = $config->getSystemValueInt('log_rotate_size', 100 * 1024 * 1024); if ($this->shouldRotateBySize()) { $rotatedFile = $this->rotate(); - $msg = 'Log file "'.$this->filePath.'" was over '.$this->maxSize.' bytes, moved to "'.$rotatedFile.'"'; + $msg = 'Log file "' . $this->filePath . '" was over ' . $this->maxSize . ' bytes, moved to "' . $rotatedFile . '"'; \OCP\Server::get(LoggerInterface::class)->info($msg, ['app' => Rotate::class]); } } diff --git a/lib/private/Log/Syslog.php b/lib/private/Log/Syslog.php index bd2c39509b1..46214599eb8 100644 --- a/lib/private/Log/Syslog.php +++ b/lib/private/Log/Syslog.php @@ -20,15 +20,18 @@ class Syslog extends LogDetails implements IWriter { ILogger::FATAL => LOG_CRIT, ]; + private string $tag; + public function __construct( SystemConfig $config, ?string $tag = null, ) { parent::__construct($config); if ($tag === null) { - $tag = $config->getValue('syslog_tag', 'Nextcloud'); + $this->tag = $config->getValue('syslog_tag', 'Nextcloud'); + } else { + $this->tag = $tag; } - openlog($tag, LOG_PID | LOG_CONS, LOG_USER); } public function __destruct() { @@ -41,6 +44,7 @@ class Syslog extends LogDetails implements IWriter { */ public function write(string $app, $message, int $level): void { $syslog_level = $this->levels[$level]; + openlog($this->tag, LOG_PID | LOG_CONS, LOG_USER); syslog($syslog_level, $this->logDetailsAsJSON($app, $message, $level)); } } diff --git a/lib/private/Log/Systemdlog.php b/lib/private/Log/Systemdlog.php index 4e33a4e5419..ffea0511732 100644 --- a/lib/private/Log/Systemdlog.php +++ b/lib/private/Log/Systemdlog.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later @@ -58,8 +59,8 @@ class Systemdlog extends LogDetails implements IWriter { */ public function write(string $app, $message, int $level): void { $journal_level = $this->levels[$level]; - sd_journal_send('PRIORITY='.$journal_level, - 'SYSLOG_IDENTIFIER='.$this->syslogId, + sd_journal_send('PRIORITY=' . $journal_level, + 'SYSLOG_IDENTIFIER=' . $this->syslogId, 'MESSAGE=' . $this->logDetailsAsJSON($app, $message, $level)); } } |