12 December 2023

To define a custom hook_watchdog in Drupal 7, you'll need to create a custom module. The hook_watchdog function allows you to log messages to the watchdog database table, which is useful for debugging and logging events in your Drupal site.

Implement the hook_watchdog function

Edit your "custom_watchdog.module" file and implement the hook_watchdog function. Here's a simple example:
Source code viewer
  1. /**
  2.  * Implements hook_watchdog().
  3.  */
  4. function custom_watchdog_watchdog($log_entry) {
  5. // Your custom logic goes here.
  6. // $log_entry is an associative array containing the log entry details.
  8. // Example: Log to the Drupal watchdog table.
  9. watchdog('custom_watchdog', $log_entry['message'], $log_entry['severity'], $log_entry['link']);
  10. }
Programming Language: PHP

Test your custom watchdog log

You can now use your custom watchdog log by calling watchdog in your module or wherever you need to log messages.
Source code viewer
  1. // Example usage in your custom module.
  2. watchdog('custom_watchdog', 'Custom log message', array(), WATCHDOG_NOTICE);
Programming Language: PHP