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 protected]";
$from = "[email protected]";
$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;
}
}