LoggerChannel.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. 'user' => NULL,
  92. 'uid' => 0,
  93. 'request_uri' => '',
  94. 'referer' => '',
  95. 'ip' => '',
  96. 'timestamp' => time(),
  97. ];
  98. // Some context values are only available when in a request context.
  99. if ($this->requestStack && $request = $this->requestStack->getCurrentRequest()) {
  100. $context['request_uri'] = $request->getUri();
  101. $context['referer'] = $request->headers->get('Referer', '');
  102. $context['ip'] = $request->getClientIP();
  103. try {
  104. if ($this->currentUser) {
  105. $context['user'] = $this->currentUser;
  106. $context['uid'] = $this->currentUser->id();
  107. }
  108. }
  109. catch (\Exception $e) {
  110. // An exception might be thrown if the database connection is not
  111. // available or due to another unexpected reason. It is more important
  112. // to log the error that we already have so any additional exceptions
  113. // are ignored.
  114. }
  115. }
  116. if (is_string($level)) {
  117. // Convert to integer equivalent for consistency with RFC 5424.
  118. $level = $this->levelTranslation[$level];
  119. }
  120. // Call all available loggers.
  121. foreach ($this->sortLoggers() as $logger) {
  122. $logger->log($level, $message, $context);
  123. }
  124. $this->callDepth--;
  125. }
  126. /**
  127. * {@inheritdoc}
  128. */
  129. public function setRequestStack(RequestStack $requestStack = NULL) {
  130. $this->requestStack = $requestStack;
  131. }
  132. /**
  133. * {@inheritdoc}
  134. */
  135. public function setCurrentUser(AccountInterface $current_user = NULL) {
  136. $this->currentUser = $current_user;
  137. }
  138. /**
  139. * {@inheritdoc}
  140. */
  141. public function setLoggers(array $loggers) {
  142. $this->loggers = $loggers;
  143. }
  144. /**
  145. * {@inheritdoc}
  146. */
  147. public function addLogger(LoggerInterface $logger, $priority = 0) {
  148. $this->loggers[$priority][] = $logger;
  149. }
  150. /**
  151. * Sorts loggers according to priority.
  152. *
  153. * @return array
  154. * An array of sorted loggers by priority.
  155. */
  156. protected function sortLoggers() {
  157. $sorted = [];
  158. krsort($this->loggers);
  159. foreach ($this->loggers as $loggers) {
  160. $sorted = array_merge($sorted, $loggers);
  161. }
  162. return $sorted;
  163. }
  164. }