Logger.php 5.0 KB

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