diff --git a/core/lib/Drupal/Core/Logger/LoggerChannel.php b/core/lib/Drupal/Core/Logger/LoggerChannel.php index 9f16a61..aa812ed 100644 --- a/core/lib/Drupal/Core/Logger/LoggerChannel.php +++ b/core/lib/Drupal/Core/Logger/LoggerChannel.php @@ -90,15 +90,15 @@ public function log($level, $message, array $context = array()) { 'ip' => '', 'timestamp' => time(), ); - if ($this->currentUser) { - $context['user'] = $this->currentUser; - $context['uid'] = $this->currentUser->id(); - } // Some context values are only available when in a request context. if ($this->requestStack && $request = $this->requestStack->getCurrentRequest()) { $context['request_uri'] = $request->getUri(); $context['referer'] = $request->headers->get('Referer', ''); $context['ip'] = $request->getClientIP(); + if ($this->currentUser) { + $context['user'] = $this->currentUser; + $context['uid'] = $this->currentUser->id(); + } } if (is_string($level)) { diff --git a/core/lib/Drupal/Core/Logger/LoggerChannelFactory.php b/core/lib/Drupal/Core/Logger/LoggerChannelFactory.php index 328828e..98724ca 100644 --- a/core/lib/Drupal/Core/Logger/LoggerChannelFactory.php +++ b/core/lib/Drupal/Core/Logger/LoggerChannelFactory.php @@ -39,16 +39,12 @@ public function get($channel) { if (!isset($this->channels[$channel])) { $instance = new LoggerChannel($channel); - // If the service container is set and request is available, set it with - // the current user to the channel. + // If we have a container set the request_stack and current_user services + // on the channel. It is up to the channel to determine if there is a + // current request. if ($this->container) { - try { - $instance->setRequestStack($this->container->get('request_stack')); - $instance->setCurrentUser($this->container->get('current_user')); - } - catch (RuntimeException $e) { - // We are not in a request context. - } + $instance->setRequestStack($this->container->get('request_stack')); + $instance->setCurrentUser($this->container->get('current_user')); } // Pass the loggers to the channel. diff --git a/core/tests/Drupal/Tests/Core/Logger/LoggerChannelTest.php b/core/tests/Drupal/Tests/Core/Logger/LoggerChannelTest.php index 3366287..5fda5cb 100644 --- a/core/tests/Drupal/Tests/Core/Logger/LoggerChannelTest.php +++ b/core/tests/Drupal/Tests/Core/Logger/LoggerChannelTest.php @@ -132,10 +132,11 @@ function ($context) { return $context['channel'] == 'test' && empty($contex['uid']) && empty($context['ip']); }, ); - // With account but not request. + // With account but not request. Since the request is not available the + // current user should not be used. $cases [] = array( function ($context) { - return $context['uid'] === 1 && empty($context['ip']); + return $context['uid'] === 0 && empty($context['ip']); }, NULL, $account_mock,