diff options
Diffstat (limited to 'apps/workflowengine/lib/Service')
-rw-r--r-- | apps/workflowengine/lib/Service/Logger.php | 172 | ||||
-rw-r--r-- | apps/workflowengine/lib/Service/RuleMatcher.php | 50 |
2 files changed, 221 insertions, 1 deletions
diff --git a/apps/workflowengine/lib/Service/Logger.php b/apps/workflowengine/lib/Service/Logger.php new file mode 100644 index 00000000000..8b90e6fa159 --- /dev/null +++ b/apps/workflowengine/lib/Service/Logger.php @@ -0,0 +1,172 @@ +<?php +declare(strict_types=1); +/** + * @copyright Copyright (c) 2020 Arthur Schiwon <blizzz@arthur-schiwon.de> + * + * @author Arthur Schiwon <blizzz@arthur-schiwon.de> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OCA\WorkflowEngine\Service; + +use OCA\WorkflowEngine\AppInfo\Application; +use OCA\WorkflowEngine\Helper\LogContext; +use OCP\IConfig; +use OCP\ILogger; +use OCP\Log\IDataLogger; +use OCP\Log\ILogFactory; + +class Logger { + /** @var ILogger */ + protected $generalLogger; + /** @var ILogger */ + protected $flowLogger; + /** @var IConfig */ + private $config; + /** @var ILogFactory */ + private $logFactory; + + public function __construct(ILogger $generalLogger, IConfig $config, ILogFactory $logFactory) { + $this->generalLogger = $generalLogger; + $this->config = $config; + $this->logFactory = $logFactory; + + $this->initLogger(); + } + + protected function initLogger() { + $default = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/flow.log'; + $logFile = trim((string)$this->config->getAppValue(Application::APP_ID, 'logfile', $default)); + if($logFile !== '') { + $this->flowLogger = $this->logFactory->getCustomLogger($logFile); + } + } + + public function logFlowRequests(LogContext $logContext) { + $message = 'Flow activation: rules were requested for operation {op}'; + $context = ['op' => $logContext->getDetails()['operation']['name']]; + + $logContext->setDescription('Flow activation: rules were requested'); + + $this->log($message, $context, $logContext); + } + + public function logScopeExpansion(LogContext $logContext) { + $message = 'Flow rule of a different user is legit for operation {op}'; + $context = ['op' => $logContext->getDetails()['operation']['name']]; + + $logContext->setDescription('Flow rule of a different user is legit'); + + $this->log($message, $context, $logContext); + } + + public function logPassedCheck(LogContext $logContext) { + $message = 'Flow rule qualified to run {op}, config: {config}'; + $context = [ + 'op' => $logContext->getDetails()['operation']['name'], + 'config' => $logContext->getDetails()['configuration'], + ]; + + $logContext->setDescription('Flow rule qualified to run'); + + $this->log($message, $context, $logContext); + } + + public function logRunSingle(LogContext $logContext) { + $message = 'Last qualified flow configuration is going to run {op}'; + $context = [ + 'op' => $logContext->getDetails()['operation']['name'], + ]; + + $logContext->setDescription('Last qualified flow configuration is going to run'); + + $this->log($message, $context, $logContext); + } + + public function logRunAll(LogContext $logContext) { + $message = 'All qualified flow configurations are going to run {op}'; + $context = [ + 'op' => $logContext->getDetails()['operation']['name'], + ]; + + $logContext->setDescription('All qualified flow configurations are going to run'); + + $this->log($message, $context, $logContext); + } + + public function logRunNone(LogContext $logContext) { + $message = 'No flow configurations is going to run {op}'; + $context = [ + 'op' => $logContext->getDetails()['operation']['name'], + ]; + + $logContext->setDescription('No flow configurations is going to run'); + + $this->log($message, $context, $logContext); + } + + public function logEventInit(LogContext $logContext) { + $message = 'Flow activated by event {ev}'; + + $context = [ + 'ev' => $logContext->getDetails()['eventName'], + ]; + + $logContext->setDescription('Flow activated by event'); + + $this->log($message, $context, $logContext); + } + + public function logEventDone(LogContext $logContext) { + $message = 'Flow handling done for event {ev}'; + + $context = [ + 'ev' => $logContext->getDetails()['eventName'], + ]; + + $logContext->setDescription('Flow handling for event done'); + + $this->log($message, $context, $logContext); + } + + protected function log( + string $message, + array $context, + LogContext $logContext + ): void + { + if(!isset($context['app'])) { + $context['app'] = Application::APP_ID; + } + if(!isset($context['level'])) { + $context['level'] = ILogger::INFO; + } + $this->generalLogger->log($context['level'], $message, $context); + + if(!$this->flowLogger instanceof IDataLogger) { + return; + } + + $details = $logContext->getDetails(); + $this->flowLogger->logData( + $details['message'], + $details, + ['app' => Application::APP_ID, 'level' => $context['level']] + ); + } +} diff --git a/apps/workflowengine/lib/Service/RuleMatcher.php b/apps/workflowengine/lib/Service/RuleMatcher.php index 57e3813c309..d73fe752f74 100644 --- a/apps/workflowengine/lib/Service/RuleMatcher.php +++ b/apps/workflowengine/lib/Service/RuleMatcher.php @@ -24,6 +24,7 @@ declare(strict_types=1); namespace OCA\WorkflowEngine\Service; +use OCA\WorkflowEngine\Helper\LogContext; use OCA\WorkflowEngine\Helper\ScopeContext; use OCA\WorkflowEngine\Manager; use OCP\AppFramework\QueryException; @@ -58,17 +59,21 @@ class RuleMatcher implements IRuleMatcher { protected $operation; /** @var IEntity */ protected $entity; + /** @var Logger */ + protected $logger; public function __construct( IUserSession $session, IServerContainer $container, IL10N $l, - Manager $manager + Manager $manager, + Logger $logger ) { $this->session = $session; $this->manager = $manager; $this->container = $container; $this->l = $l; + $this->logger = $logger; } public function setFileInfo(IStorage $storage, string $path, bool $isDir = false): void { @@ -116,6 +121,13 @@ class RuleMatcher implements IRuleMatcher { $scopes[] = new ScopeContext(IManager::SCOPE_USER, $user->getUID()); } + $ctx = new LogContext(); + $ctx + ->setScopes($scopes) + ->setEntity($this->entity) + ->setOperation($this->operation); + $this->logger->logFlowRequests($ctx); + $operations = []; foreach ($scopes as $scope) { $operations = array_merge($operations, $this->manager->getOperations($class, $scope)); @@ -129,6 +141,12 @@ class RuleMatcher implements IRuleMatcher { continue; } if ($this->entity->isLegitimatedForUserId($scopeCandidate->getScopeId())) { + $ctx = new LogContext(); + $ctx + ->setScopes($scopeCandidate) + ->setEntity($this->entity) + ->setOperation($this->operation); + $this->logger->logScopeExpansion($ctx); $operations = array_merge($operations, $this->manager->getOperations($class, $scopeCandidate)); } } @@ -146,12 +164,36 @@ class RuleMatcher implements IRuleMatcher { } } + $ctx = new LogContext(); + $ctx + ->setEntity($this->entity) + ->setOperation($this->operation) + ->setConfiguration($operation); + $this->logger->logPassedCheck($ctx); + if ($returnFirstMatchingOperationOnly) { + $ctx = new LogContext(); + $ctx + ->setEntity($this->entity) + ->setOperation($this->operation) + ->setConfiguration($operation); + $this->logger->logRunSingle($ctx); return $operation; } $matches[] = $operation; } + $ctx = new LogContext(); + $ctx + ->setEntity($this->entity) + ->setOperation($this->operation); + if(!empty($matches)) { + $ctx->setConfiguration($matches); + $this->logger->logRunAll($ctx); + } else { + $this->logger->logRunNone($ctx); + } + return $matches; } @@ -183,4 +225,10 @@ class RuleMatcher implements IRuleMatcher { } return $checkInstance->executeCheck($check['operator'], $check['value']); } + + protected function logCandidate() { + $logContext = new LogContext(); + $logContext + ->setOperation(); + } } |