logo

Custom code to save value in error log in Drupal 9

20
March

Custom code to save value in error log in Drupal 9
By: Anonymous | Published On: Mon, 03/20/2023 - 15:40

To save values in the error log in Drupal 8, you can use the following custom code:

\Drupal::logger('my_module')->error('The error message: @variable', [
  '@variable' => $value_to_save,
]);

In the code above, replace my_module with the name of your module, and replace The error message with the message you want to include in the log entry. You can include any number of placeholders in the message using the @variable syntax.

The $value_to_save variable should contain the value you want to log. You can replace this with any variable or expression that produces the value you want to log.

The error method is used to log the message as an error entry in the log. You can also use debug, info, notice, warning, or critical methods depending on the severity of the log entry.

The log entries are stored in the watchdog table in the Drupal database. You can view them in the Reports > Recent log messages page in the Drupal admin interface.

Need Help ?