Pull merge.
[yaffs-website] / web / core / lib / Drupal / Core / Logger / LoggerChannelTrait.php
1 <?php
2
3 namespace Drupal\Core\Logger;
4
5 /**
6  * Wrapper methods for the logger factory service.
7  *
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.
12  *
13  * @see \Drupal\Core\DependencyInjection\ContainerInjectionInterface
14  */
15 trait LoggerChannelTrait {
16
17   /**
18    * The logger channel factory service.
19    *
20    * @var \Drupal\Core\Logger\LoggerChannelFactoryInterface
21    */
22   protected $loggerFactory;
23
24   /**
25    * Gets the logger for a specific channel.
26    *
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.
30    *
31    * @return \Psr\Log\LoggerInterface
32    *   The logger for the given channel.
33    *
34    * @todo Require the use of injected services:
35    *   https://www.drupal.org/node/2733703
36    */
37   protected function getLogger($channel) {
38     if (!$this->loggerFactory) {
39       $this->loggerFactory = \Drupal::service('logger.factory');
40     }
41     return $this->loggerFactory->get($channel);
42   }
43
44   /**
45    * Injects the logger channel factory.
46    *
47    * @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory
48    *   The logger channel factory service.
49    *
50    * @return $this
51    */
52   public function setLoggerFactory(LoggerChannelFactoryInterface $logger_factory) {
53     $this->loggerFactory = $logger_factory;
54
55     return $this;
56   }
57
58 }