Drupal 8 : Logging Messages and Errors

  1. Notice / Warnings Messages - Function notice():

Notice or warning messages are logged by calling the function notice().

Syntax: 

\Drupal::logger(‘MODULE_NAME’’)->notice($message, $valueArray); 

$message : First parameter to the function, Its logging message.

$valueArray : Optional parameter, Holds the value passed to the message. Each identifier are given as array key to values.

Example:  

\Drupal::logger('Faq_Ask')->notice("Asker notification email sent to @to for question @quest", array('@to' => $email, '@quest' => SafeMarkup::checkPlain($node_title))); 

    2. Error Messages - Function error():

Error messages are logged by calling the function error().

Syntax:  

 \Drupal::logger(‘MODULE_NAME’’)->error($message, $valueArray); 

$message : First parameter to the function, Its logging message.

$valueArray : Optional parameter, Holds the value passed to the message. Each identifier are given as array key to values.

Example:  

\Drupal::logger('Faq_Ask')->error('Asker notification email to @to failed for the "@quest" question.', array('@to' => $email, '@quest' => SafeMarkup::checkPlain($node_title))); 

 

Category: