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.
You can show all errors by adding a few lines to your local testing site's settings. php: error_reporting(E_ALL); ini_set('display_errors', TRUE); ini_set('display_startup_errors', TRUE); In addition, navigate to Administration→ Configuration→ Development → logging and errors and select "All messages".
To turn on the “Error Messages to Display”, go to the Administration menu, then go into Configuration > Development > Logging and errors (/admin/config/development/logging). You will find the following configuration. There are 4 options available, and the default setting is “None”.
To view entries in Drupal's own internal log system (the watchdog database table), go to http://example.com/admin/reports/dblog. These can include Drupal-specific errors as well as general PHP or MySQL errors that have been thrown. Use the watchdog() function to add an entry to this log from your own custom module.
Developers familiar with Drupal 7 will also be familiar with watchdog(), an API function that allows you to create a log message that appears on the Reports page in the Drupal administrative UI. Implementing D7's hook_watchdog allows module developers to customize the destination of these log messages.
You can use the watchdog
function :
watchdog($type, $message, $variables = array(), $severity = WATCHDOG_NOTICE, $link = NULL)
Quoting the manual, the parameters are :
$type
The category to which this message belongs.$message
The message to store in the log.$variables
Array of variables to replace in the message on display or NULL if message is already translated or not possible to translate.$severity
The severity of the message, as per RFC 3164$link
A link to associate with the message.And the error levels can be found on the page of watchdog_severity_levels
. For an error, you'll most probably use WATCHDOG_ERROR
, or maybe even something more "critical", depending on the kind of error.
Drupal 8
// Logs a notice \Drupal::logger('my_module')->notice($message); // Logs an error \Drupal::logger('my_module')->error($message);
See more examples at How to Log Messages in Drupal 8.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With