EventLogger.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Piotr Mrówczyński <mrow4a@yahoo.com>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OC\Diagnostics;
  26. use OC\Log;
  27. use OC\SystemConfig;
  28. use OCP\Diagnostics\IEvent;
  29. use OCP\Diagnostics\IEventLogger;
  30. use Psr\Log\LoggerInterface;
  31. class EventLogger implements IEventLogger {
  32. /** @var Event[] */
  33. private $events = [];
  34. /** @var SystemConfig */
  35. private $config;
  36. /** @var LoggerInterface */
  37. private $logger;
  38. /** @var Log */
  39. private $internalLogger;
  40. /**
  41. * @var bool - Module needs to be activated by some app
  42. */
  43. private $activated = false;
  44. public function __construct(SystemConfig $config, LoggerInterface $logger, Log $internalLogger) {
  45. $this->config = $config;
  46. $this->logger = $logger;
  47. $this->internalLogger = $internalLogger;
  48. if ($this->isLoggingActivated()) {
  49. $this->activate();
  50. }
  51. }
  52. public function isLoggingActivated(): bool {
  53. $systemValue = (bool)$this->config->getValue('diagnostics.logging', false)
  54. || (bool)$this->config->getValue('profiler', false);
  55. if ($systemValue && $this->config->getValue('debug', false)) {
  56. return true;
  57. }
  58. $isDebugLevel = $this->internalLogger->getLogLevel([]) === Log::DEBUG;
  59. return $systemValue && $isDebugLevel;
  60. }
  61. /**
  62. * @inheritdoc
  63. */
  64. public function start($id, $description = '') {
  65. if ($this->activated) {
  66. $this->events[$id] = new Event($id, $description, microtime(true));
  67. $this->writeLog($this->events[$id]);
  68. }
  69. }
  70. /**
  71. * @inheritdoc
  72. */
  73. public function end($id) {
  74. if ($this->activated && isset($this->events[$id])) {
  75. $timing = $this->events[$id];
  76. $timing->end(microtime(true));
  77. $this->writeLog($timing);
  78. }
  79. }
  80. /**
  81. * @inheritdoc
  82. */
  83. public function log($id, $description, $start, $end) {
  84. if ($this->activated) {
  85. $this->events[$id] = new Event($id, $description, $start);
  86. $this->events[$id]->end($end);
  87. $this->writeLog($this->events[$id]);
  88. }
  89. }
  90. /**
  91. * @inheritdoc
  92. */
  93. public function getEvents() {
  94. return $this->events;
  95. }
  96. /**
  97. * @inheritdoc
  98. */
  99. public function activate() {
  100. $this->activated = true;
  101. }
  102. private function writeLog(IEvent $event) {
  103. if ($this->activated) {
  104. if ($event->getEnd() === null) {
  105. return;
  106. }
  107. $duration = $event->getDuration();
  108. $timeInMs = round($duration * 1000, 4);
  109. $loggingMinimum = (int)$this->config->getValue('diagnostics.logging.threshold', 0);
  110. if ($loggingMinimum === 0 || $timeInMs < $loggingMinimum) {
  111. return;
  112. }
  113. $message = microtime() . ' - ' . $event->getId() . ': ' . $timeInMs . ' (' . $event->getDescription() . ')';
  114. $this->logger->debug($message, ['app' => 'diagnostics']);
  115. }
  116. }
  117. }