Drupal Debugging Tips: How to use watchdog()

| | 1 min read

The simplest debugging tool used most often by a programmer is a print statement, which when placed, used wisely can bring out the weak points of any giant bug.

In the context of Drupal web development, the output of print statements often goes buried under huge piles of html output. In such cases, it is useful to have an alternative debug message channel, something which does not get mixed up in html. To help the programmer in this context, Drupal offers watchdog(), a function that logs debug messages to databases.

watchdog() is a frequently used drupal function to output messages to dblog. The most common usage is

watchdog('my_module',"Message %number: %message", 
    array("number"=>10, "message"=>"The message to display")); 

       // the third parameter is optional, if the message is already formatted.

This will output "Message 10: The message to display" under the my_module in /admin/reports/dblog

See http://api.drupal.org/api/drupal/includes%21bootstrap.inc/function/watchdog/7