How to log error message in drupal

Viewed 58783

How to log our own error messages(for ex: error due to invalid user date entry) which is generated in php program to drupal error log.

7 Answers

Both watchdog for D7 & \Drupal::logger for D8 will write log in watchdog table (in your database), and with HUGE data logged, you can imagine performance impact.

You can use error_log php function to do it (see PHP manual).

error_log("Your message", 3, "/path/to/your/log/file.log");

You need to have permission to write in your log file (/path/to/your/log/file.log)

// Get logger factory.
$logger = \Drupal::service('logger.factory');

// Log a message with dynamic variables.
$nodeType = 'Article';
$userName = 'Admin';
$logger->get($moduleName)->notice('A new "@nodeType" created by %userName.', [
    '@nodeType' => $nodeType,
    '%userName' => $userName,
]);

Source

Related