How to Create and Use a Custom Logger Channel in Drupal

Custom logger channels let you group your module's log messages under a specific name. This keeps your logs organized and makes debugging custom code much faster.

calendar_today
folder_openCore

In this quick tutorial, we will set up a custom logger channel for a module called mint_core and inject it directly into a service.

Step 1: Register the Channel in services.yml

Drupal makes creating a custom channel effortless by providing a logger.channel_base abstract service. You simply need to extend it and pass your channel's machine name as an argument.

In your module's mint_core.services.yml file, define the channel service and pass it as a dependency to your custom service:

descriptionmint_core.services.yml
1services:
2  # 1. Define the custom logger channel
3  logger.channel.mint_core:
4    parent: logger.channel_base
5    arguments: ['mint_core']
6
7  # 2. Inject the channel into your service
8  mint_core.example:
9    class: Drupal\mint_core\Service\ExampleService
10    arguments: ['@logger.channel.mint_core']
lightbulb
Naming Convention Tip: Naming your channel service logger.channel.[module_name] follows Drupal core conventions. This allows Drupal's container to automatically wire or recognize your channel when needed.

Step 2: Inject the Logger Channel into Your Class

Now, open (or create) your service class at src/Service/ExampleService.php. Type-hint the injected argument against Drupal\Core\Logger\LoggerChannelInterface:

descriptionsrc/Service/ExampleService.php
1<?php
2
3namespace Drupal\mint_core\Service;
4
5use Drupal\Core\Logger\LoggerChannelInterface;
6
7/**
8 * Provides an example service utilizing a custom logger channel.
9 */
10class ExampleService {
11
12  /**
13   * Constructs an ExampleService object.
14   */
15  public function __construct(
16    protected LoggerChannelInterface $logger,
17  ) {}
18
19  /**
20   * Executes business logic and records a log entry.
21   */
22  public function doSomething(): void {
23    // Perform your logic here...
24
25    // Log a notice to the 'mint_core' channel
26    $this->logger->notice('Something important happened in the mint_core service!');
27  }
28
29}

Because LoggerChannelInterface implements PSR-3 standard logging methods, you can use $this->logger->info(), $this->logger->error(), $this->logger->warning(), and more out of the box.

Step 3: Test and See It in Action

After creating or updating your services file, rebuild your site cache so Drupal can register the new service definition:

drush cr

Quick Verification via Drush

To quickly verify that everything works without needing a controller or event subscriber, create a temporary PHP file in your project root (e.g., test-logger.php) with the following code:

codePHP
1// Test snippet executed via: drush php-script /path/to/test.php
2use Drupal\mint_core\Service\ExampleService;
3
4/** @var \Drupal\mint_core\Service\ExampleService $service */
5$service = \Drupal::service('mint_core.example');
6$service->doSomething();

Then, execute the script from your terminal using Drush's `php-script` command:

drush php-script test-logger.php

Or using the shorthand alias:

drush scr test-logger.php

Now head over to Reports > Recent log messages (/admin/reports/dblog) in your Drupal admin toolbar. Filter by Type "mint_core", and you will see your log entry grouped under its own clean channel!

Tags

ModulesLogging