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.

Share it!

There’s just one person behind this website; if this article was helpful (or interesting), I’d appreciate it if you’d share it. Thanks, Al.

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.