Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. LoggerChannelTrait.php

trait LoggerChannelTrait

Wrapper methods for the logger factory service.

This utility trait should only be used in application-level code, such as classes that would implement ContainerInjectionInterface. Services registered in the Container should not use this trait but inject the appropriate service directly for easier testing.

Hierarchy

  • trait \Drupal\Core\Logger\LoggerChannelTrait

See also

\Drupal\Core\DependencyInjection\ContainerInjectionInterface

7 files declare their use of LoggerChannelTrait
ControllerBase.php in core/lib/Drupal/Core/Controller/ControllerBase.php
FieldBlockDeriver.php in core/modules/layout_builder/src/Plugin/Derivative/FieldBlockDeriver.php
FormBase.php in core/lib/Drupal/Core/Form/FormBase.php
ImageItem.php in core/modules/image/src/Plugin/Field/FieldType/ImageItem.php
LanguageNegotiator.php in core/modules/language/src/LanguageNegotiator.php

... See full list

File

core/lib/Drupal/Core/Logger/LoggerChannelTrait.php, line 15

Namespace

Drupal\Core\Logger
View source
trait LoggerChannelTrait {
    
    /**
     * The logger channel factory service.
     *
     * @var \Drupal\Core\Logger\LoggerChannelFactoryInterface
     */
    protected $loggerFactory;
    
    /**
     * Gets the logger for a specific channel.
     *
     * @param string $channel
     *   The name of the channel. Can be any string, but the general practice is
     *   to use the name of the subsystem calling this.
     *
     * @return \Psr\Log\LoggerInterface
     *   The logger for the given channel.
     *
     * @todo Require the use of injected services:
     *   https://www.drupal.org/node/2733703
     */
    protected function getLogger($channel) {
        if (!$this->loggerFactory) {
            $this->loggerFactory = \Drupal::service('logger.factory');
        }
        return $this->loggerFactory
            ->get($channel);
    }
    
    /**
     * Injects the logger channel factory.
     *
     * @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory
     *   The logger channel factory service.
     *
     * @return $this
     */
    public function setLoggerFactory(LoggerChannelFactoryInterface $logger_factory) {
        $this->loggerFactory = $logger_factory;
        return $this;
    }

}

Members

Title Sort descending Modifiers Object type Summary
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal