diff options
Diffstat (limited to 'apps/admin_audit/lib/Actions/Action.php')
-rw-r--r-- | apps/admin_audit/lib/Actions/Action.php | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/apps/admin_audit/lib/Actions/Action.php b/apps/admin_audit/lib/Actions/Action.php new file mode 100644 index 00000000000..acd415d82ea --- /dev/null +++ b/apps/admin_audit/lib/Actions/Action.php @@ -0,0 +1,66 @@ +<?php + +declare(strict_types=1); +/** + * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ +namespace OCA\AdminAudit\Actions; + +use OCA\AdminAudit\IAuditLogger; + +class Action { + + public function __construct( + private IAuditLogger $logger, + ) { + } + + /** + * Log a single action with a log level of info + * + * @param string $text + * @param array $params + * @param array $elements + * @param bool $obfuscateParameters + */ + public function log(string $text, + array $params, + array $elements, + bool $obfuscateParameters = false): void { + foreach ($elements as $element) { + if (!isset($params[$element])) { + if ($obfuscateParameters) { + $this->logger->critical( + '$params["' . $element . '"] was missing.', + ['app' => 'admin_audit'] + ); + } else { + $this->logger->critical( + '$params["' . $element . '"] was missing. Transferred value: {params}', + ['app' => 'admin_audit', 'params' => $params] + ); + } + return; + } + } + + $replaceArray = []; + foreach ($elements as $element) { + if ($params[$element] instanceof \DateTime) { + $params[$element] = $params[$element]->format('Y-m-d H:i:s'); + } + $replaceArray[] = $params[$element]; + } + + $this->logger->info( + vsprintf( + $text, + $replaceArray + ), + [ + 'app' => 'admin_audit' + ] + ); + } +} |