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:
watchdog($type, $message, $variables = array(), $severity = WATCHDOG_NOTICE, $link = NULL)
The watchdog function parameters are described in the link above, and are repeated here for your convenience:
Here's a link to more information on the watchdog severity levels.
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:
watchdog('mail', 'Email injection exploit attempted in email form subject: ' . check_plain($form_state['values']['subject']), WATCHDOG_NOTICE);
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:
watchdog('workflow', 'Attempt to go to nonexistent transition (from %old to %new)', array('%old' => $old_sid, '%new' => $sid, WATCHDOG_ERROR));
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.