1
0

PsrLoggerAdapter.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 OC\Log;
  8. use OC\Log;
  9. use OCP\EventDispatcher\IEventDispatcher;
  10. use OCP\ILogger;
  11. use OCP\Log\IDataLogger;
  12. use Psr\Log\InvalidArgumentException;
  13. use Psr\Log\LoggerInterface;
  14. use Psr\Log\LogLevel;
  15. use Throwable;
  16. use function array_key_exists;
  17. use function array_merge;
  18. final class PsrLoggerAdapter implements LoggerInterface, IDataLogger {
  19. public function __construct(
  20. private Log $logger,
  21. ) {
  22. }
  23. public static function logLevelToInt(string $level): int {
  24. return match ($level) {
  25. LogLevel::ALERT => ILogger::ERROR,
  26. LogLevel::CRITICAL => ILogger::ERROR,
  27. LogLevel::DEBUG => ILogger::DEBUG,
  28. LogLevel::EMERGENCY => ILogger::FATAL,
  29. LogLevel::ERROR => ILogger::ERROR,
  30. LogLevel::INFO => ILogger::INFO,
  31. LogLevel::NOTICE => ILogger::INFO,
  32. LogLevel::WARNING => ILogger::WARN,
  33. default => throw new InvalidArgumentException('Unsupported custom log level'),
  34. };
  35. }
  36. public function setEventDispatcher(IEventDispatcher $eventDispatcher): void {
  37. $this->logger->setEventDispatcher($eventDispatcher);
  38. }
  39. private function containsThrowable(array $context): bool {
  40. return array_key_exists('exception', $context) && $context['exception'] instanceof Throwable;
  41. }
  42. /**
  43. * System is unusable.
  44. *
  45. * @param $message
  46. * @param mixed[] $context
  47. */
  48. public function emergency($message, array $context = []): void {
  49. $this->log(LogLevel::EMERGENCY, (string)$message, $context);
  50. }
  51. /**
  52. * Action must be taken immediately.
  53. *
  54. * Example: Entire website down, database unavailable, etc. This should
  55. * trigger the SMS alerts and wake you up.
  56. *
  57. * @param $message
  58. * @param mixed[] $context
  59. */
  60. public function alert($message, array $context = []): void {
  61. $this->log(LogLevel::ALERT, (string)$message, $context);
  62. }
  63. /**
  64. * Critical conditions.
  65. *
  66. * Example: Application component unavailable, unexpected exception.
  67. *
  68. * @param $message
  69. * @param mixed[] $context
  70. */
  71. public function critical($message, array $context = []): void {
  72. $this->log(LogLevel::CRITICAL, (string)$message, $context);
  73. }
  74. /**
  75. * Runtime errors that do not require immediate action but should typically
  76. * be logged and monitored.
  77. *
  78. * @param $message
  79. * @param mixed[] $context
  80. */
  81. public function error($message, array $context = []): void {
  82. $this->log(LogLevel::ERROR, (string)$message, $context);
  83. }
  84. /**
  85. * Exceptional occurrences that are not errors.
  86. *
  87. * Example: Use of deprecated APIs, poor use of an API, undesirable things
  88. * that are not necessarily wrong.
  89. *
  90. * @param $message
  91. * @param mixed[] $context
  92. */
  93. public function warning($message, array $context = []): void {
  94. $this->log(LogLevel::WARNING, (string)$message, $context);
  95. }
  96. /**
  97. * Normal but significant events.
  98. *
  99. * @param $message
  100. * @param mixed[] $context
  101. */
  102. public function notice($message, array $context = []): void {
  103. $this->log(LogLevel::NOTICE, (string)$message, $context);
  104. }
  105. /**
  106. * Interesting events.
  107. *
  108. * Example: User logs in, SQL logs.
  109. *
  110. * @param $message
  111. * @param mixed[] $context
  112. */
  113. public function info($message, array $context = []): void {
  114. $this->log(LogLevel::INFO, (string)$message, $context);
  115. }
  116. /**
  117. * Detailed debug information.
  118. *
  119. * @param $message
  120. * @param mixed[] $context
  121. */
  122. public function debug($message, array $context = []): void {
  123. $this->log(LogLevel::DEBUG, (string)$message, $context);
  124. }
  125. /**
  126. * Logs with an arbitrary level.
  127. *
  128. * @param mixed $level
  129. * @param $message
  130. * @param mixed[] $context
  131. *
  132. * @throws InvalidArgumentException
  133. */
  134. public function log($level, $message, array $context = []): void {
  135. if (is_string($level)) {
  136. $level = self::logLevelToInt($level);
  137. }
  138. if (isset($context['level']) && is_string($context['level'])) {
  139. $context['level'] = self::logLevelToInt($context['level']);
  140. }
  141. if (!is_int($level) || $level < ILogger::DEBUG || $level > ILogger::FATAL) {
  142. throw new InvalidArgumentException('Unsupported custom log level');
  143. }
  144. if ($this->containsThrowable($context)) {
  145. $this->logger->logException($context['exception'], array_merge(
  146. [
  147. 'message' => (string)$message,
  148. 'level' => $level,
  149. ],
  150. $context
  151. ));
  152. } else {
  153. $this->logger->log($level, (string)$message, $context);
  154. }
  155. }
  156. public function logData(string $message, array $data, array $context = []): void {
  157. $this->logger->logData($message, $data, $context);
  158. }
  159. }