Logger.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. /** @var ILogger */
  36. protected $generalLogger;
  37. /** @var LoggerInterface */
  38. protected $flowLogger;
  39. /** @var IConfig */
  40. private $config;
  41. /** @var ILogFactory */
  42. private $logFactory;
  43. public function __construct(ILogger $generalLogger, IConfig $config, ILogFactory $logFactory) {
  44. $this->generalLogger = $generalLogger;
  45. $this->config = $config;
  46. $this->logFactory = $logFactory;
  47. $this->initLogger();
  48. }
  49. protected function initLogger() {
  50. $default = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/flow.log';
  51. $logFile = trim((string)$this->config->getAppValue(Application::APP_ID, 'logfile', $default));
  52. if ($logFile !== '') {
  53. $this->flowLogger = $this->logFactory->getCustomPsrLogger($logFile);
  54. }
  55. }
  56. public function logFlowRequests(LogContext $logContext) {
  57. $message = 'Flow activation: rules were requested for operation {op}';
  58. $context = ['op' => $logContext->getDetails()['operation']['name'], 'level' => ILogger::DEBUG];
  59. $logContext->setDescription('Flow activation: rules were requested');
  60. $this->log($message, $context, $logContext);
  61. }
  62. public function logScopeExpansion(LogContext $logContext) {
  63. $message = 'Flow rule of a different user is legit for operation {op}';
  64. $context = ['op' => $logContext->getDetails()['operation']['name']];
  65. $logContext->setDescription('Flow rule of a different user is legit');
  66. $this->log($message, $context, $logContext);
  67. }
  68. public function logPassedCheck(LogContext $logContext) {
  69. $message = 'Flow rule qualified to run {op}, config: {config}';
  70. $context = [
  71. 'op' => $logContext->getDetails()['operation']['name'],
  72. 'config' => $logContext->getDetails()['configuration'],
  73. 'level' => ILogger::DEBUG,
  74. ];
  75. $logContext->setDescription('Flow rule qualified to run');
  76. $this->log($message, $context, $logContext);
  77. }
  78. public function logRunSingle(LogContext $logContext) {
  79. $message = 'Last qualified flow configuration is going to run {op}';
  80. $context = [
  81. 'op' => $logContext->getDetails()['operation']['name'],
  82. ];
  83. $logContext->setDescription('Last qualified flow configuration is going to run');
  84. $this->log($message, $context, $logContext);
  85. }
  86. public function logRunAll(LogContext $logContext) {
  87. $message = 'All qualified flow configurations are going to run {op}';
  88. $context = [
  89. 'op' => $logContext->getDetails()['operation']['name'],
  90. ];
  91. $logContext->setDescription('All qualified flow configurations are going to run');
  92. $this->log($message, $context, $logContext);
  93. }
  94. public function logRunNone(LogContext $logContext) {
  95. $message = 'No flow configurations is going to run {op}';
  96. $context = [
  97. 'op' => $logContext->getDetails()['operation']['name'],
  98. 'level' => ILogger::DEBUG,
  99. ];
  100. $logContext->setDescription('No flow configurations is going to run');
  101. $this->log($message, $context, $logContext);
  102. }
  103. public function logEventInit(LogContext $logContext) {
  104. $message = 'Flow activated by event {ev}';
  105. $context = [
  106. 'ev' => $logContext->getDetails()['eventName'],
  107. 'level' => ILogger::DEBUG,
  108. ];
  109. $logContext->setDescription('Flow activated by event');
  110. $this->log($message, $context, $logContext);
  111. }
  112. public function logEventDone(LogContext $logContext) {
  113. $message = 'Flow handling done for event {ev}';
  114. $context = [
  115. 'ev' => $logContext->getDetails()['eventName'],
  116. ];
  117. $logContext->setDescription('Flow handling for event done');
  118. $this->log($message, $context, $logContext);
  119. }
  120. protected function log(
  121. string $message,
  122. array $context,
  123. LogContext $logContext
  124. ): void {
  125. if (!isset($context['app'])) {
  126. $context['app'] = Application::APP_ID;
  127. }
  128. if (!isset($context['level'])) {
  129. $context['level'] = ILogger::INFO;
  130. }
  131. $this->generalLogger->log($context['level'], $message, $context);
  132. if (!$this->flowLogger instanceof IDataLogger) {
  133. return;
  134. }
  135. $details = $logContext->getDetails();
  136. $this->flowLogger->logData(
  137. $details['message'],
  138. $details,
  139. ['app' => Application::APP_ID, 'level' => $context['level']]
  140. );
  141. }
  142. }