4

I would like to tell watchdog not to write the "Cron run completed" to the log while leaving all other messages intact. How can I do this?

Thanks!

3 Answers 3

9

There is no special hook for this. The easiest way to fix this in Drupal 8 is overriding logger.dblog service.

According to the official documentation you need implement a class extending ServiceProviderBase and the alter() method as shown below.

<?php

namespace Drupal\example;

use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\DependencyInjection\ServiceProviderBase;

/**
 * Modifies the logger.dblog service.
 */
class ExampleServiceProvider extends ServiceProviderBase {

  /**
   * {@inheritdoc}
   */
  public function alter(ContainerBuilder $container) {
    // Overrides logger.dblog service to exclude some unwanted log messages.
    $container->getDefinition('logger.dblog')
      ->setClass('Drupal\example\Logger\DbLog');
  }

}

The logger itself is extremely simple.

<?php

namespace Drupal\example\Logger;

use Drupal\dblog\Logger\DbLog as BaseDbLog;

/**
 * Logs events in the watchdog database table.
 */
class DbLog extends BaseDbLog {

  /**
   * {@inheritdoc}
   */
  public function log($level, $message, array $context = []) {
    if ($message !== 'Cron run completed.') {
      parent::log($level, $message, $context);
    }
  }

}
 
1
  • Thanks. I am using Drupal 7 for the project where I want to suppress the messages... Any way to do that there? Dec 9, 2016 at 11:59
2

For drupal 7 you could implement hook_watchdog() in a custom module to see the error messages but it will not allow you to change the messages logged into database. Read more about hook_watchdog here

For deleting specific messages perhaps you could implement hook_cron in your own module using something similar to what watchdog does when cleaning itself from excess messages as seen here but you have to adjust the code to find only specific messages to delete.

Perhaps viewing all log message except the ones you mentioned can be done with https://www.drupal.org/project/views_watchdog which allows you to create a view of dblog message and probably you can use views filtering to filter them out.

Or https://www.drupal.org/project/watchdog_filtering might be able to do the filtering for you

2
  • Thank you very much GiorgosK! So many ideas - I will check them all and comment here again. Amir May 1, 2017 at 12:27
  • Hi, I have chosen the Views Watchdog module - thanks again. May 1, 2017 at 22:51
0

Instead of trying to not write a specific log message, you can also ue the Views Watchdog module (to not include such messages when creating a list of log messages).

Some details about this module (from its project page):

... extends the Views module and allows to create customized lists (pages, blocks, feeds) of watchdog entries.

Features

This module provides several fields, filter and sort criteria to build custom pages, blocks, and feeds.

It also provides the style Watchdog table to display watchdog entries as highlighted rows in a table.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.