Logger.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\WorkflowEngine\Service;
  8. use OCA\WorkflowEngine\AppInfo\Application;
  9. use OCA\WorkflowEngine\Helper\LogContext;
  10. use OCP\IConfig;
  11. use OCP\ILogger;
  12. use OCP\Log\IDataLogger;
  13. use OCP\Log\ILogFactory;
  14. use Psr\Log\LoggerInterface;
  15. class Logger {
  16. protected ?LoggerInterface $flowLogger = null;
  17. public function __construct(
  18. protected LoggerInterface $generalLogger,
  19. private IConfig $config,
  20. private ILogFactory $logFactory,
  21. ) {
  22. $this->initLogger();
  23. }
  24. protected function initLogger(): void {
  25. $default = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/flow.log';
  26. $logFile = trim((string)$this->config->getAppValue(Application::APP_ID, 'logfile', $default));
  27. if ($logFile !== '') {
  28. $this->flowLogger = $this->logFactory->getCustomPsrLogger($logFile);
  29. }
  30. }
  31. public function logFlowRequests(LogContext $logContext) {
  32. $message = 'Flow activation: rules were requested for operation {op}';
  33. $context = ['op' => $logContext->getDetails()['operation']['name'], 'level' => ILogger::DEBUG];
  34. $logContext->setDescription('Flow activation: rules were requested');
  35. $this->log($message, $context, $logContext);
  36. }
  37. public function logScopeExpansion(LogContext $logContext) {
  38. $message = 'Flow rule of a different user is legit for operation {op}';
  39. $context = ['op' => $logContext->getDetails()['operation']['name']];
  40. $logContext->setDescription('Flow rule of a different user is legit');
  41. $this->log($message, $context, $logContext);
  42. }
  43. public function logPassedCheck(LogContext $logContext) {
  44. $message = 'Flow rule qualified to run {op}, config: {config}';
  45. $context = [
  46. 'op' => $logContext->getDetails()['operation']['name'],
  47. 'config' => $logContext->getDetails()['configuration'],
  48. 'level' => ILogger::DEBUG,
  49. ];
  50. $logContext->setDescription('Flow rule qualified to run');
  51. $this->log($message, $context, $logContext);
  52. }
  53. public function logRunSingle(LogContext $logContext) {
  54. $message = 'Last qualified flow configuration is going to run {op}';
  55. $context = [
  56. 'op' => $logContext->getDetails()['operation']['name'],
  57. ];
  58. $logContext->setDescription('Last qualified flow configuration is going to run');
  59. $this->log($message, $context, $logContext);
  60. }
  61. public function logRunAll(LogContext $logContext) {
  62. $message = 'All qualified flow configurations are going to run {op}';
  63. $context = [
  64. 'op' => $logContext->getDetails()['operation']['name'],
  65. ];
  66. $logContext->setDescription('All qualified flow configurations are going to run');
  67. $this->log($message, $context, $logContext);
  68. }
  69. public function logRunNone(LogContext $logContext) {
  70. $message = 'No flow configurations is going to run {op}';
  71. $context = [
  72. 'op' => $logContext->getDetails()['operation']['name'],
  73. 'level' => ILogger::DEBUG,
  74. ];
  75. $logContext->setDescription('No flow configurations is going to run');
  76. $this->log($message, $context, $logContext);
  77. }
  78. public function logEventInit(LogContext $logContext) {
  79. $message = 'Flow activated by event {ev}';
  80. $context = [
  81. 'ev' => $logContext->getDetails()['eventName'],
  82. 'level' => ILogger::DEBUG,
  83. ];
  84. $logContext->setDescription('Flow activated by event');
  85. $this->log($message, $context, $logContext);
  86. }
  87. public function logEventDone(LogContext $logContext) {
  88. $message = 'Flow handling done for event {ev}';
  89. $context = [
  90. 'ev' => $logContext->getDetails()['eventName'],
  91. ];
  92. $logContext->setDescription('Flow handling for event done');
  93. $this->log($message, $context, $logContext);
  94. }
  95. protected function log(
  96. string $message,
  97. array $context,
  98. LogContext $logContext
  99. ): void {
  100. if (!isset($context['app'])) {
  101. $context['app'] = Application::APP_ID;
  102. }
  103. if (!isset($context['level'])) {
  104. $context['level'] = ILogger::INFO;
  105. }
  106. $this->generalLogger->log($context['level'], $message, $context);
  107. if (!$this->flowLogger instanceof IDataLogger) {
  108. return;
  109. }
  110. $details = $logContext->getDetails();
  111. $this->flowLogger->logData(
  112. $details['message'],
  113. $details,
  114. ['app' => Application::APP_ID, 'level' => $context['level']]
  115. );
  116. }
  117. }