annotate core/lib/Drupal/Core/Logger/LoggerChannelTrait.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Core\Logger;
Chris@0 4
Chris@0 5 /**
Chris@0 6 * Wrapper methods for the logger factory service.
Chris@0 7 *
Chris@0 8 * This utility trait should only be used in application-level code, such as
Chris@0 9 * classes that would implement ContainerInjectionInterface. Services registered
Chris@0 10 * in the Container should not use this trait but inject the appropriate service
Chris@0 11 * directly for easier testing.
Chris@0 12 *
Chris@0 13 * @see \Drupal\Core\DependencyInjection\ContainerInjectionInterface
Chris@0 14 */
Chris@0 15 trait LoggerChannelTrait {
Chris@0 16
Chris@0 17 /**
Chris@0 18 * The logger channel factory service.
Chris@0 19 *
Chris@0 20 * @var \Drupal\Core\Logger\LoggerChannelFactoryInterface
Chris@0 21 */
Chris@0 22 protected $loggerFactory;
Chris@0 23
Chris@0 24 /**
Chris@0 25 * Gets the logger for a specific channel.
Chris@0 26 *
Chris@0 27 * @param string $channel
Chris@0 28 * The name of the channel. Can be any string, but the general practice is
Chris@0 29 * to use the name of the subsystem calling this.
Chris@0 30 *
Chris@0 31 * @return \Psr\Log\LoggerInterface
Chris@0 32 * The logger for the given channel.
Chris@0 33 *
Chris@0 34 * @todo Require the use of injected services:
Chris@0 35 * https://www.drupal.org/node/2733703
Chris@0 36 */
Chris@0 37 protected function getLogger($channel) {
Chris@0 38 if (!$this->loggerFactory) {
Chris@0 39 $this->loggerFactory = \Drupal::service('logger.factory');
Chris@0 40 }
Chris@0 41 return $this->loggerFactory->get($channel);
Chris@0 42 }
Chris@0 43
Chris@0 44 /**
Chris@0 45 * Injects the logger channel factory.
Chris@0 46 *
Chris@0 47 * @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory
Chris@0 48 * The logger channel factory service.
Chris@0 49 *
Chris@0 50 * @return $this
Chris@0 51 */
Chris@0 52 public function setLoggerFactory(LoggerChannelFactoryInterface $logger_factory) {
Chris@0 53 $this->loggerFactory = $logger_factory;
Chris@0 54
Chris@0 55 return $this;
Chris@0 56 }
Chris@0 57
Chris@0 58 }