LoggerChannel.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace Drupal\Core\Logger;
  3. use Drupal\Core\Session\AccountInterface;
  4. use Psr\Log\LoggerInterface;
  5. use Psr\Log\LoggerTrait;
  6. use Psr\Log\LogLevel;
  7. use Symfony\Component\HttpFoundation\RequestStack;
  8. /**
  9. * Defines a logger channel that most implementations will use.
  10. */
  11. class LoggerChannel implements LoggerChannelInterface {
  12. use LoggerTrait;
  13. /**
  14. * Maximum call depth to self::log() for a single log message.
  15. *
  16. * It's very easy for logging channel code to call out to other library code
  17. * that will create log messages. In that case, we will recurse back in to
  18. * LoggerChannel::log() multiple times while processing a single originating
  19. * message. To prevent infinite recursion, we track the call depth and bail
  20. * out at LoggerChannel::MAX_CALL_DEPTH iterations.
  21. *
  22. * @var int
  23. */
  24. const MAX_CALL_DEPTH = 5;
  25. /**
  26. * Number of times LoggerChannel::log() has been called for a single message.
  27. *
  28. * @var int
  29. */
  30. protected $callDepth = 0;
  31. /**
  32. * The name of the channel of this logger instance.
  33. *
  34. * @var string
  35. */
  36. protected $channel;
  37. /**
  38. * Map of PSR3 log constants to RFC 5424 log constants.
  39. *
  40. * @var array
  41. */
  42. protected $levelTranslation = [
  43. LogLevel::EMERGENCY => RfcLogLevel::EMERGENCY,
  44. LogLevel::ALERT => RfcLogLevel::ALERT,
  45. LogLevel::CRITICAL => RfcLogLevel::CRITICAL,
  46. LogLevel::ERROR => RfcLogLevel::ERROR,
  47. LogLevel::WARNING => RfcLogLevel::WARNING,
  48. LogLevel::NOTICE => RfcLogLevel::NOTICE,
  49. LogLevel::INFO => RfcLogLevel::INFO,
  50. LogLevel::DEBUG => RfcLogLevel::DEBUG,
  51. ];
  52. /**
  53. * An array of arrays of \Psr\Log\LoggerInterface keyed by priority.
  54. *
  55. * @var array
  56. */
  57. protected $loggers = [];
  58. /**
  59. * The request stack object.
  60. *
  61. * @var \Symfony\Component\HttpFoundation\RequestStack
  62. */
  63. protected $requestStack;
  64. /**
  65. * The current user object.
  66. *
  67. * @var \Drupal\Core\Session\AccountInterface
  68. */
  69. protected $currentUser;
  70. /**
  71. * Constructs a LoggerChannel object
  72. *
  73. * @param string $channel
  74. * The channel name for this instance.
  75. */
  76. public function __construct($channel) {
  77. $this->channel = $channel;
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. public function log($level, $message, array $context = []) {
  83. if ($this->callDepth == self::MAX_CALL_DEPTH) {
  84. return;
  85. }
  86. $this->callDepth++;
  87. // Merge in defaults.
  88. $context += [
  89. 'channel' => $this->channel,
  90. 'link' => '',
  91. 'uid' => 0,
  92. 'request_uri' => '',
  93. 'referer' => '',
  94. 'ip' => '',
  95. 'timestamp' => time(),
  96. ];
  97. // Some context values are only available when in a request context.
  98. if ($this->requestStack && $request = $this->requestStack->getCurrentRequest()) {
  99. $context['request_uri'] = $request->getUri();
  100. $context['referer'] = $request->headers->get('Referer', '');
  101. $context['ip'] = $request->getClientIP();
  102. if ($this->currentUser) {
  103. $context['uid'] = $this->currentUser->id();
  104. }
  105. }
  106. if (is_string($level)) {
  107. // Convert to integer equivalent for consistency with RFC 5424.
  108. $level = $this->levelTranslation[$level];
  109. }
  110. // Call all available loggers.
  111. foreach ($this->sortLoggers() as $logger) {
  112. $logger->log($level, $message, $context);
  113. }
  114. $this->callDepth--;
  115. }
  116. /**
  117. * {@inheritdoc}
  118. */
  119. public function setRequestStack(RequestStack $requestStack = NULL) {
  120. $this->requestStack = $requestStack;
  121. }
  122. /**
  123. * {@inheritdoc}
  124. */
  125. public function setCurrentUser(AccountInterface $current_user = NULL) {
  126. $this->currentUser = $current_user;
  127. }
  128. /**
  129. * {@inheritdoc}
  130. */
  131. public function setLoggers(array $loggers) {
  132. $this->loggers = $loggers;
  133. }
  134. /**
  135. * {@inheritdoc}
  136. */
  137. public function addLogger(LoggerInterface $logger, $priority = 0) {
  138. $this->loggers[$priority][] = $logger;
  139. }
  140. /**
  141. * Sorts loggers according to priority.
  142. *
  143. * @return array
  144. * An array of sorted loggers by priority.
  145. */
  146. protected function sortLoggers() {
  147. $sorted = [];
  148. krsort($this->loggers);
  149. foreach ($this->loggers as $loggers) {
  150. $sorted = array_merge($sorted, $loggers);
  151. }
  152. return $sorted;
  153. }
  154. }