diff --git a/core/lib/Drupal/Core/DependencyInjection/Compiler/RegisterLoggersPass.php b/core/lib/Drupal/Core/DependencyInjection/Compiler/RegisterLoggersPass.php index 65b691d..087275b 100644 --- a/core/lib/Drupal/Core/DependencyInjection/Compiler/RegisterLoggersPass.php +++ b/core/lib/Drupal/Core/DependencyInjection/Compiler/RegisterLoggersPass.php @@ -21,7 +21,10 @@ public function process(ContainerBuilder $container) { $definitions = array(); foreach ($container->findTaggedServiceIds('logger_channel') as $id => $attributes) { $definition = $container->getDefinition($id); + // Set the request object. $definition->addMethodCall('setRequest', array(new Reference('request', ContainerInterface::IGNORE_ON_INVALID_REFERENCE))); + // Set the current_user object. + $definition->addMethodCall('setCurrentUser', array(new Reference('current_user', ContainerInterface::IGNORE_ON_INVALID_REFERENCE))); $definitions[] = $definition; } // Loop through all available logger services (eg dblog, syslog) and add diff --git a/core/lib/Drupal/Core/Logger/LoggerChannel.php b/core/lib/Drupal/Core/Logger/LoggerChannel.php index 704bb35..0e32da6 100644 --- a/core/lib/Drupal/Core/Logger/LoggerChannel.php +++ b/core/lib/Drupal/Core/Logger/LoggerChannel.php @@ -7,6 +7,7 @@ namespace Drupal\Core\Logger; +use Drupal\Core\Session\AccountInterface; use Psr\Log\AbstractLogger; use Psr\Log\LoggerInterface; use Psr\Log\LogLevel; @@ -44,6 +45,13 @@ class LoggerChannel extends AbstractLogger { protected $request; /** + * The current user object. + * + * @var \Drupal\Core\Session\AccountInterface + */ + protected $currentUser; + + /** * Constructs a LoggerChannel object * * @param string $channel @@ -68,10 +76,12 @@ 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->request) { - $context['user'] = $this->request->attributes->get('_account'); - $context['uid'] = isset($context['user']) ? $context['user']->id() : 0; $context['request_uri'] = $this->request->getUri(); $context['referer'] = $this->request->headers->get('Referer', ''); $context['ip'] = $this->request->getClientIP(); @@ -98,6 +108,16 @@ public function setRequest(Request $request = NULL) { } /** + * Sets the current user. + * + * @param \Drupal\Core\Session\AccountInterface $current_user + * The current user object. + */ + public function setCurrentUser(AccountInterface $current_user = NULL) { + $this->currentUser = $current_user; + } + + /** * Adds a logger. * * @param \Psr\Log\LoggerInterface $logger diff --git a/core/lib/Drupal/Core/Logger/LoggerChannelFactory.php b/core/lib/Drupal/Core/Logger/LoggerChannelFactory.php index a53f743..397521b 100644 --- a/core/lib/Drupal/Core/Logger/LoggerChannelFactory.php +++ b/core/lib/Drupal/Core/Logger/LoggerChannelFactory.php @@ -27,9 +27,8 @@ public function getLogger($channel) { if ($this->container->has($channel_service)) { return $this->container->get($channel_service); } - else { - return $this->container->get('logger.channel.default'); - } + + return $this->container->get('logger.channel.default'); } } diff --git a/core/modules/system/tests/modules/entity_cache_test/entity_cache_test.module b/core/modules/system/tests/modules/entity_cache_test/entity_cache_test.module index 192003f..9a0676d 100644 --- a/core/modules/system/tests/modules/entity_cache_test/entity_cache_test.module +++ b/core/modules/system/tests/modules/entity_cache_test/entity_cache_test.module @@ -6,7 +6,7 @@ */ /** - * Implements hook_modules_enabled(). + * Implements hook_modules_installed(). * * This hook is called during \Drupal\Core\Extension\ModuleHandler::install() * and since this hook implementation is invoked, we have to expect that this @@ -16,7 +16,7 @@ * * @see EntityApiInfoTest::testEntityInfoCacheModulesEnabled() */ -function entity_cache_test_modules_enabled($modules_enabled) { +function entity_cache_test_modules_installed($modules_enabled) { $info = entity_get_info('entity_cache_test'); // Store the information in a system variable to analyze it later in the // test case.