123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- <?php
- declare(strict_types=1);
- /**
- * @copyright Copyright (c) 2020 Arthur Schiwon <blizzz@arthur-schiwon.de>
- *
- * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
- * @author Christoph Wurst <christoph@winzerhof-wurst.at>
- * @author Julius Härtl <jus@bitgrid.net>
- *
- * @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;
- use Psr\Log\LoggerInterface;
- class Logger {
- protected ?LoggerInterface $flowLogger = null;
- public function __construct(
- protected LoggerInterface $generalLogger,
- private IConfig $config,
- private ILogFactory $logFactory,
- ) {
- $this->initLogger();
- }
- protected function initLogger(): void {
- $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->getCustomPsrLogger($logFile);
- }
- }
- public function logFlowRequests(LogContext $logContext) {
- $message = 'Flow activation: rules were requested for operation {op}';
- $context = ['op' => $logContext->getDetails()['operation']['name'], 'level' => ILogger::DEBUG];
- $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'],
- 'level' => ILogger::DEBUG,
- ];
- $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'],
- 'level' => ILogger::DEBUG,
- ];
- $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'],
- 'level' => ILogger::DEBUG,
- ];
- $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']]
- );
- }
- }
|