1<?php 2 3declare(strict_types=1); 4 5/** 6 * @copyright Copyright (c) 2020 Arthur Schiwon <blizzz@arthur-schiwon.de> 7 * 8 * @author Arthur Schiwon <blizzz@arthur-schiwon.de> 9 * @author Christoph Wurst <christoph@winzerhof-wurst.at> 10 * @author Julius Härtl <jus@bitgrid.net> 11 * 12 * @license GNU AGPL version 3 or any later version 13 * 14 * This program is free software: you can redistribute it and/or modify 15 * it under the terms of the GNU Affero General Public License as 16 * published by the Free Software Foundation, either version 3 of the 17 * License, or (at your option) any later version. 18 * 19 * This program is distributed in the hope that it will be useful, 20 * but WITHOUT ANY WARRANTY; without even the implied warranty of 21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22 * GNU Affero General Public License for more details. 23 * 24 * You should have received a copy of the GNU Affero General Public License 25 * along with this program. If not, see <http://www.gnu.org/licenses/>. 26 * 27 */ 28namespace OCA\WorkflowEngine\Service; 29 30use OCA\WorkflowEngine\AppInfo\Application; 31use OCA\WorkflowEngine\Helper\LogContext; 32use OCP\IConfig; 33use OCP\ILogger; 34use OCP\Log\IDataLogger; 35use OCP\Log\ILogFactory; 36use Psr\Log\LoggerInterface; 37 38class Logger { 39 /** @var ILogger */ 40 protected $generalLogger; 41 /** @var LoggerInterface */ 42 protected $flowLogger; 43 /** @var IConfig */ 44 private $config; 45 /** @var ILogFactory */ 46 private $logFactory; 47 48 public function __construct(ILogger $generalLogger, IConfig $config, ILogFactory $logFactory) { 49 $this->generalLogger = $generalLogger; 50 $this->config = $config; 51 $this->logFactory = $logFactory; 52 53 $this->initLogger(); 54 } 55 56 protected function initLogger() { 57 $default = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/flow.log'; 58 $logFile = trim((string)$this->config->getAppValue(Application::APP_ID, 'logfile', $default)); 59 if ($logFile !== '') { 60 $this->flowLogger = $this->logFactory->getCustomPsrLogger($logFile); 61 } 62 } 63 64 public function logFlowRequests(LogContext $logContext) { 65 $message = 'Flow activation: rules were requested for operation {op}'; 66 $context = ['op' => $logContext->getDetails()['operation']['name'], 'level' => ILogger::DEBUG]; 67 68 $logContext->setDescription('Flow activation: rules were requested'); 69 70 $this->log($message, $context, $logContext); 71 } 72 73 public function logScopeExpansion(LogContext $logContext) { 74 $message = 'Flow rule of a different user is legit for operation {op}'; 75 $context = ['op' => $logContext->getDetails()['operation']['name']]; 76 77 $logContext->setDescription('Flow rule of a different user is legit'); 78 79 $this->log($message, $context, $logContext); 80 } 81 82 public function logPassedCheck(LogContext $logContext) { 83 $message = 'Flow rule qualified to run {op}, config: {config}'; 84 $context = [ 85 'op' => $logContext->getDetails()['operation']['name'], 86 'config' => $logContext->getDetails()['configuration'], 87 'level' => ILogger::DEBUG, 88 ]; 89 90 $logContext->setDescription('Flow rule qualified to run'); 91 92 $this->log($message, $context, $logContext); 93 } 94 95 public function logRunSingle(LogContext $logContext) { 96 $message = 'Last qualified flow configuration is going to run {op}'; 97 $context = [ 98 'op' => $logContext->getDetails()['operation']['name'], 99 ]; 100 101 $logContext->setDescription('Last qualified flow configuration is going to run'); 102 103 $this->log($message, $context, $logContext); 104 } 105 106 public function logRunAll(LogContext $logContext) { 107 $message = 'All qualified flow configurations are going to run {op}'; 108 $context = [ 109 'op' => $logContext->getDetails()['operation']['name'], 110 ]; 111 112 $logContext->setDescription('All qualified flow configurations are going to run'); 113 114 $this->log($message, $context, $logContext); 115 } 116 117 public function logRunNone(LogContext $logContext) { 118 $message = 'No flow configurations is going to run {op}'; 119 $context = [ 120 'op' => $logContext->getDetails()['operation']['name'], 121 'level' => ILogger::DEBUG, 122 ]; 123 124 $logContext->setDescription('No flow configurations is going to run'); 125 126 $this->log($message, $context, $logContext); 127 } 128 129 public function logEventInit(LogContext $logContext) { 130 $message = 'Flow activated by event {ev}'; 131 132 $context = [ 133 'ev' => $logContext->getDetails()['eventName'], 134 'level' => ILogger::DEBUG, 135 ]; 136 137 $logContext->setDescription('Flow activated by event'); 138 139 $this->log($message, $context, $logContext); 140 } 141 142 public function logEventDone(LogContext $logContext) { 143 $message = 'Flow handling done for event {ev}'; 144 145 $context = [ 146 'ev' => $logContext->getDetails()['eventName'], 147 ]; 148 149 $logContext->setDescription('Flow handling for event done'); 150 151 $this->log($message, $context, $logContext); 152 } 153 154 protected function log( 155 string $message, 156 array $context, 157 LogContext $logContext 158 ): void { 159 if (!isset($context['app'])) { 160 $context['app'] = Application::APP_ID; 161 } 162 if (!isset($context['level'])) { 163 $context['level'] = ILogger::INFO; 164 } 165 $this->generalLogger->log($context['level'], $message, $context); 166 167 if (!$this->flowLogger instanceof IDataLogger) { 168 return; 169 } 170 171 $details = $logContext->getDetails(); 172 $this->flowLogger->logData( 173 $details['message'], 174 $details, 175 ['app' => Application::APP_ID, 'level' => $context['level']] 176 ); 177 } 178} 179