diff --git a/core/core.services.yml b/core/core.services.yml index ccab45b..3d0c165 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -129,8 +129,7 @@ services: arguments: ['@database'] logger.factory: class: Drupal\Core\Logger\LoggerChannelFactory - calls: - - [setContainer, ['@service_container']] + arguments: ['@request_stack', '@current_user'] logger.channel.default: class: Drupal\Core\Logger\LoggerChannel factory_method: get diff --git a/core/lib/Drupal/Core/Logger/LoggerChannel.php b/core/lib/Drupal/Core/Logger/LoggerChannel.php index 3cc64e7..cd78f8d 100644 --- a/core/lib/Drupal/Core/Logger/LoggerChannel.php +++ b/core/lib/Drupal/Core/Logger/LoggerChannel.php @@ -11,7 +11,7 @@ use Psr\Log\LoggerInterface; use Psr\Log\LoggerTrait; use Psr\Log\LogLevel; -use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\RequestStack; /** * Defines a logger channel that most implementations will use. @@ -51,11 +51,11 @@ class LoggerChannel implements LoggerChannelInterface { protected $loggers = array(); /** - * The request object. + * The request stack. * - * @var \Symfony\Component\HttpFoundation\Request + * @var \Symfony\Component\HttpFoundation\RequestStack */ - protected $request; + protected $requestStack; /** * The current user object. @@ -69,9 +69,15 @@ class LoggerChannel implements LoggerChannelInterface { * * @param string $channel * The channel name for this instance. + * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack + * The request stack. + * @param \Drupal\Core\Session\AccountInterface $current_user + * The current user object. */ - public function __construct($channel) { + public function __construct($channel, RequestStack $request_stack, AccountInterface $current_user) { $this->channel = $channel; + $this->requestStack = $request_stack; + $this->currentUser = $current_user; } /** @@ -89,15 +95,15 @@ public function log($level, $message, array $context = array()) { 'ip' => '', 'timestamp' => time(), ); - if ($this->currentUser) { - $context['user'] = $this->currentUser; - $context['uid'] = $this->currentUser->id(); - } + + $context['user'] = $this->currentUser; + $context['uid'] = $this->currentUser->id(); // Some context values are only available when in a request context. - if ($this->request) { - $context['request_uri'] = $this->request->getUri(); - $context['referer'] = $this->request->headers->get('Referer', ''); - $context['ip'] = $this->request->getClientIP(); + $request = $this->requestStack->getCurrentRequest(); + if ($request) { + $context['request_uri'] = $request->getUri(); + $context['referer'] = $request->headers->get('Referer', ''); + $context['ip'] = $request->getClientIP(); } if (is_string($level)) { @@ -113,20 +119,6 @@ public function log($level, $message, array $context = array()) { /** * {@inheritdoc} */ - public function setRequest(Request $request = NULL) { - $this->request = $request; - } - - /** - * {@inheritdoc} - */ - public function setCurrentUser(AccountInterface $current_user = NULL) { - $this->currentUser = $current_user; - } - - /** - * {@inheritdoc} - */ public function setLoggers(array $loggers) { $this->loggers = $loggers; } diff --git a/core/lib/Drupal/Core/Logger/LoggerChannelFactory.php b/core/lib/Drupal/Core/Logger/LoggerChannelFactory.php index 58c01ba..7921923 100644 --- a/core/lib/Drupal/Core/Logger/LoggerChannelFactory.php +++ b/core/lib/Drupal/Core/Logger/LoggerChannelFactory.php @@ -7,14 +7,28 @@ namespace Drupal\Core\Logger; +use Drupal\Core\Session\AccountInterface; use Psr\Log\LoggerInterface; -use Symfony\Component\DependencyInjection\ContainerAware; -use Symfony\Component\DependencyInjection\Exception\RuntimeException; +use Symfony\Component\HttpFoundation\RequestStack; /** * Defines a factory for logging channels. */ -class LoggerChannelFactory extends ContainerAware implements LoggerChannelFactoryInterface { +class LoggerChannelFactory implements LoggerChannelFactoryInterface { + + /** + * The request stack. + * + * @var \Symfony\Component\HttpFoundation\RequestStack + */ + protected $requestStack; + + /** + * The current user object. + * + * @var \Drupal\Core\Session\AccountInterface + */ + protected $currentUser; /** * Array of all instantiated logger channels keyed by channel name. @@ -31,20 +45,24 @@ class LoggerChannelFactory extends ContainerAware implements LoggerChannelFactor protected $loggers = array(); /** + * Constructs a new LoggerChannelFactory + * + * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack + * The request stack. + * @param \Drupal\Core\Session\AccountInterface $current_user + * The current user object. + */ + public function __construct(RequestStack $request_stack, AccountInterface $current_user) { + $this->requestStack = $request_stack; + $this->currentUser = $current_user; + } + + /** * {@inheritdoc} */ public function get($channel) { if (!isset($this->channels[$channel])) { - $instance = new LoggerChannel($channel); - - // If the request is available, set it with the current user to the channel. - try { - $instance->setRequest($this->container->get('request')); - $instance->setCurrentUser($this->container->get('current_user')); - } - catch (RuntimeException $e) { - // We are not in a request context. - } + $instance = new LoggerChannel($channel, $this->requestStack, $this->currentUser); // Pass the loggers to the channel. $instance->setLoggers($this->loggers); diff --git a/core/lib/Drupal/Core/Logger/LoggerChannelInterface.php b/core/lib/Drupal/Core/Logger/LoggerChannelInterface.php index 15d9741..28af134 100644 --- a/core/lib/Drupal/Core/Logger/LoggerChannelInterface.php +++ b/core/lib/Drupal/Core/Logger/LoggerChannelInterface.php @@ -7,9 +7,7 @@ namespace Drupal\Core\Logger; -use Drupal\Core\Session\AccountInterface; use Psr\Log\LoggerInterface; -use Symfony\Component\HttpFoundation\Request; /** * Logger channel interface. @@ -17,22 +15,6 @@ interface LoggerChannelInterface extends LoggerInterface { /** - * Sets the request. - * - * @param \Symfony\Component\HttpFoundation\Request|null $request - * The current request object. - */ - public function setRequest(Request $request = NULL); - - /** - * Sets the current user. - * - * @param \Drupal\Core\Session\AccountInterface|null $current_user - * The current user object. - */ - public function setCurrentUser(AccountInterface $current_user = NULL); - - /** * Sets the loggers for this channel. * * @param array $loggers