Drupal watchdog function - How to log Drupal errors (error messages)

Drupal FAQ: How do I log Drupal errors (error messages)?

The standard way to log Drupal error messages is with the watchdog function. The watchdog function logs your messages to your database, where you can then view them from your Drupal Reports URL.

In Drupal 6 and Drupal 7 the watchdog function has this signature:

         $variables = array(), 
         $severity = WATCHDOG_NOTICE, 
         $link = NULL)

The watchdog function parameters are described in the link above, and are repeated here for your convenience:

The message to store in the log. See t() for documentation on how $message and $variables interact. Keep $message translatable by not concatenating dynamic values into it!
Array of variables to replace in the message on display or NULL if message is already translated or not possible to translate.
The severity of the message, as per RFC 3164. Possible values are WATCHDOG_ERROR, WATCHDOG_WARNING, etc.
A link to associate with the message.

Here's a link to more information on the watchdog severity levels.

Drupal watchdog examples

Given the default values shown in the function definition above, in a simple case you can call the Drupal watchdog function like this:

watchdog('my_module', 'my error message');

Here's a watchdog logging example from the Drupal email module:

         'Email injection exploit attempted in email form subject: ' .

Here's a simpler watchdog function call from the Views module:

watchdog('views_logging', '<pre>' . $output . '</pre>');

Finally, here's a watchdog function call from the Workflow module:

         'Attempt to go to nonexistent transition (from %old to %new)', 
         array('%old' => $old_sid, '%new' => $sid, WATCHDOG_ERROR));

Logging Drupal errors to syslog

On a related note, you can log Drupal error messages to syslog on Linux systems using the Drupal syslog module. There's more information on that in this Drupal syslog documentation page.