/** * Tests LoggerChannel::log(). * * @param callable $expected * An anonymous function to use with $this->callback() of the logger mock. * The function should check the $context array for expected values. * @param \Symfony\Component\HttpFoundation\Request $request * Will be passed to the channel under test if present. * @param \Drupal\Core\Session\AccountInterface $current_user * Will be passed to the channel under test if present. * * @dataProvider providerTestLog * @covers ::log * @covers ::setCurrentUser * @covers ::setRequestStack */ public function testLog(callable $expected, Request $request = NULL, AccountInterface $current_user = NULL) { $channel = new LoggerChannel('test'); $message = $this->randomMachineName(); $logger = $this->getMock('Psr\\Log\\LoggerInterface'); $logger->expects($this->once())->method('log')->with($this->anything(), $message, $this->callback($expected)); $channel->addLogger($logger); if ($request) { $requestStack = new RequestStack(); $requestStack->push($request); $channel->setRequestStack($requestStack); } if ($current_user) { $channel->setCurrentUser($current_user); } $channel->log(rand(0, 7), $message); }
/** * {@inheritdoc} */ public function get($channel) { if (!isset($this->channels[$channel])) { $instance = new LoggerChannel($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) { $instance->setRequestStack($this->container->get('request_stack')); $instance->setCurrentUser($this->container->get('current_user')); } // Pass the loggers to the channel. $instance->setLoggers($this->loggers); $this->channels[$channel] = $instance; } return $this->channels[$channel]; }