1
0

EventLogger.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Diagnostics;
  8. use OC\Log;
  9. use OC\SystemConfig;
  10. use OCP\Diagnostics\IEvent;
  11. use OCP\Diagnostics\IEventLogger;
  12. use Psr\Log\LoggerInterface;
  13. class EventLogger implements IEventLogger {
  14. /** @var Event[] */
  15. private $events = [];
  16. /** @var SystemConfig */
  17. private $config;
  18. /** @var LoggerInterface */
  19. private $logger;
  20. /** @var Log */
  21. private $internalLogger;
  22. /**
  23. * @var bool - Module needs to be activated by some app
  24. */
  25. private $activated = false;
  26. public function __construct(SystemConfig $config, LoggerInterface $logger, Log $internalLogger) {
  27. $this->config = $config;
  28. $this->logger = $logger;
  29. $this->internalLogger = $internalLogger;
  30. if ($this->isLoggingActivated()) {
  31. $this->activate();
  32. }
  33. }
  34. public function isLoggingActivated(): bool {
  35. $systemValue = (bool)$this->config->getValue('diagnostics.logging', false)
  36. || (bool)$this->config->getValue('profiler', false);
  37. if ($systemValue && $this->config->getValue('debug', false)) {
  38. return true;
  39. }
  40. $isDebugLevel = $this->internalLogger->getLogLevel([], '') === Log::DEBUG;
  41. return $systemValue && $isDebugLevel;
  42. }
  43. /**
  44. * @inheritdoc
  45. */
  46. public function start($id, $description = '') {
  47. if ($this->activated) {
  48. $this->events[$id] = new Event($id, $description, microtime(true));
  49. $this->writeLog($this->events[$id]);
  50. }
  51. }
  52. /**
  53. * @inheritdoc
  54. */
  55. public function end($id) {
  56. if ($this->activated && isset($this->events[$id])) {
  57. $timing = $this->events[$id];
  58. $timing->end(microtime(true));
  59. $this->writeLog($timing);
  60. }
  61. }
  62. /**
  63. * @inheritdoc
  64. */
  65. public function log($id, $description, $start, $end) {
  66. if ($this->activated) {
  67. $this->events[$id] = new Event($id, $description, $start);
  68. $this->events[$id]->end($end);
  69. $this->writeLog($this->events[$id]);
  70. }
  71. }
  72. /**
  73. * @inheritdoc
  74. */
  75. public function getEvents() {
  76. return $this->events;
  77. }
  78. /**
  79. * @inheritdoc
  80. */
  81. public function activate() {
  82. $this->activated = true;
  83. }
  84. private function writeLog(IEvent $event) {
  85. if ($this->activated) {
  86. if ($event->getEnd() === null) {
  87. return;
  88. }
  89. $duration = $event->getDuration();
  90. $timeInMs = round($duration * 1000, 4);
  91. $loggingMinimum = (int)$this->config->getValue('diagnostics.logging.threshold', 0);
  92. if ($loggingMinimum === 0 || $timeInMs < $loggingMinimum) {
  93. return;
  94. }
  95. $message = microtime() . ' - ' . $event->getId() . ': ' . $timeInMs . ' (' . $event->getDescription() . ')';
  96. $this->logger->debug($message, ['app' => 'diagnostics']);
  97. }
  98. }
  99. }