Adding debug logs in module codefor Drupal 8 , 9 , 10 , and 11

Last updated :  

Introduction

Debugging is an essential aspect of software development, and Drupal is no exception. Integrating logging mechanisms into your module can significantly improve your ability to diagnose issues and understand your module's runtime behavior. In this lesson, we will explore how to add debug logs to your custom Drupal module, allowing for more effective troubleshooting.

Understanding Logging in Drupal

Drupal's logging system is built on the PSR-3 standard, providing a common interface for logging libraries. The default logger in Drupal is handled by the Database Log module, which logs messages to the database, making them accessible via the Reports section of the admin interface. Debug logs are generally written using this system, ensuring they are easy to locate and review.

Setting Up Logging

To add logging capabilities in your module, you will typically make use of the logger service. Let's start by identifying where you might want to add logging within a custom module.

Step-by-Step: Adding a Log Entry

Suppose we are working with a module named my_module. Within this module, you might have a function that processes some data and needs logging capabilities:

Step 1: Inject the Logger Service

First, inject the logger service into your class. This is done by adding the @logger service in your class constructor, allowing you to log messages:


namespace Drupal\my_module;

use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;

class MyModuleService implements ContainerInjectionInterface {

  protected $logger;

  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('logger.factory')->get('my_module')
    );
  }

  public function __construct(LoggerInterface $logger) {
    $this->logger = $logger;
  }

  public function processData() {
    // Simulating data processing logic.
    $success = $this->performComplexLogic();

    if ($success) {
      $this->logger->info('Data processing succeeded.');
    } else {
      $this->logger->error('Data processing failed.');
    }
  }

  private function performComplexLogic() {
    // Example logic processing
    return true; // or false based on your logic
  }
}

This code sample logs an informational message when data processing succeeds and an error message if it fails.

Step 2: Utilize Different Log Levels

Drupal's logger supports multiple log levels provided by PSR-3, including:

  • debug
  • info
  • notice
  • warning
  • error
  • critical

Each log level signifies the severity or importance of the message. For most debugging purposes, you’ll use debug, info, and error. Here is the usage:


$this->logger->debug('Here is a debug message.');
// This will be used to log detailed information, generally useful for developers.

Step 3: Viewing Logs

To view the logs in Drupal, go to Reports > Recent log messages. This provides an interface to filter logs by type, severity, etc. Logs are also accessible programmatically, should you need to analyze them further.

Best Practices for Logging

  • Use appropriate log levels to ensure important messages aren't lost in routine logs.
  • Avoid logging sensitive information that could compromise user data.
  • Regularly monitor logs to catch issues early.
  • Combine logs with Drupal permissions to control who can view log entries.

Conclusion

By adding log entries in your Drupal modules, you can effectively monitor and troubleshoot your code, making your development and debugging process smoother. Logs inform you about the normal operation of your module and alert you to potential issues.

What's Next?

Now that you've learned the basics of logging in Drupal, we will further enhance our debugging skills in the next lesson by integrating the Devel module. This powerful toolset allows us to debug more extensively and efficiently, opening up more sophisticated methods for diagnosing problems in your modules.