3 namespace Drupal\Core\Logger;
6 * Wrapper methods for the logger factory service.
8 * This utility trait should only be used in application-level code, such as
9 * classes that would implement ContainerInjectionInterface. Services registered
10 * in the Container should not use this trait but inject the appropriate service
11 * directly for easier testing.
13 * @see \Drupal\Core\DependencyInjection\ContainerInjectionInterface
15 trait LoggerChannelTrait {
18 * The logger channel factory service.
20 * @var \Drupal\Core\Logger\LoggerChannelFactoryInterface
22 protected $loggerFactory;
25 * Gets the logger for a specific channel.
27 * @param string $channel
28 * The name of the channel. Can be any string, but the general practice is
29 * to use the name of the subsystem calling this.
31 * @return \Psr\Log\LoggerInterface
32 * The logger for the given channel.
34 * @todo Require the use of injected services:
35 * https://www.drupal.org/node/2733703
37 protected function getLogger($channel) {
38 if (!$this->loggerFactory) {
39 $this->loggerFactory = \Drupal::service('logger.factory');
41 return $this->loggerFactory->get($channel);
45 * Injects the logger channel factory.
47 * @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory
48 * The logger channel factory service.
52 public function setLoggerFactory(LoggerChannelFactoryInterface $logger_factory) {
53 $this->loggerFactory = $logger_factory;