0

I was looking into the new Events system for Drupal 8 and there's something I haven't been able to figure out.

I need to dispatch an event when a user logs something to the watchdog table using the new logging API.

// Logs a notice
\Drupal::logger('system')->notice($message);
// Logs an error
\Drupal::logger('system')->error($message);

https://www.drupal.org/docs/8/api/logging-api/overview

I already define a new Event in a custom module but I don't know what needs to be created so when a module logs/adds information to the watchdog table using the code above my event gets triggered.

Thanks

  • 1
    That is still a hook, not an event (hook_user_login()) – Berdir Apr 30 '18 at 7:57
  • So you have a custom module that logs a message and at the same time fires a custom 'event'? Unless I misunderstand, have you looked at creating an event listener? symfony.com/doc/current/event_dispatcher.html – BWagner Apr 30 '18 at 15:07
  • Hi guys, thanks for your answer. I don't really want to log a message with my custom module, I want my module to react when a message has been log on the watchdog table. In other words, I need to trigger a function in my custom module when an error is logged in the watchdog table. – Esnare Maussa May 7 '18 at 7:27
  • When In ran the drupal debug:event, I can't see an event for the logger class. So I don't know how to create an event which gets trigger when Drupal core logs in an error on the watchdog table. Does someone knows how to do that one? :) – Esnare Maussa May 7 '18 at 7:32
0

Once you get the event working, all you have to do is create an instance of the Logger Class in the Logger folder inside your module.

In here: /drupal_root/web/module/custom/my_module/src/Logger

And the class will be something like:

namespace Drupal\my_module\Logger;


use Drupal\Core\Logger\RfcLoggerTrait;
use Psr\Log\LoggerInterface;

class SendToEmail implements LoggerInterface {

  use RfcLoggerTrait;

  /**
   * Logs with an arbitrary level.
   *
   * @param mixed $level
   * @param string $message
   * @param array $context
   *
   * @return void
   */
  public function log($level, $message, array $context = []) {
    //Building the parameters for the email
    $params['level'] = $level;
    $params['message'] = $message;
    $params['context'] = $context;
    $this->sendEmail($params);
  }

  /**
   * @param array $params
   *
   * @return null
   */
  public function sendEmail($params = array()){
    //Variable declaration
    $module = 'my_module';
    $key = 'my_module_message';
    $to = "email@email.com";
    $from = "email@email.com";
    $language_code = \Drupal::service('language_manager')->getDefaultLanguage()->getId();
    $send_now = TRUE;

   $mail = \Drupal::service('plugin.manager.mail')->mail($module, $key, $to, $language_code, $params, $from, $send_now);
    return $mail;
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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