2

I am about to develop a custom module that will hook into a hook that is triggered when cron runs. Since the module will be activated during a cron run, i am not sure how to go about giving myself feedback as I develop. If I was working with javascript I could use console.log(myvar) to log information about variables etc.

If I was developing a module that ran on a page I could use the devel module and use dpm() in my module code.

However since this module will run with cron, dpm() doesn't seem like the best way of doing this.

From searching around I see that watchdog('error title', 'error message'); could have been a good way of doing this in Drupal 6.

What is the Drupal 7 way of logging messages during custom module development?

  • 1
    You can use watchdog function in Drupal 7, as well. – Елин Й. Jun 24 '14 at 16:25
  • You might also be interested in drupal_debug()/dd() from devel. It can be useful when dpm() doesn't display any output. – Andy Jun 25 '14 at 13:35
3

The function watchdog is also available in Drupal 7

Here is an exemple of how you can use it in your case :

watchdog('MyModule', 'My message, this is my node title : %node_title', array('%node_title' => $node->title), WATCHDOG_INFO, NULL);

If you want to use a translated string you can do something like this :

watchdog('MyModule', t('My message, this is my node title : @node_title', array('@node_title' => $node->title)), array(), WATCHDOG_INFO, NULL);

If you need to print an array, you can try something like this :

watchdog('MyModule', '<pre>'. print_r($node, TRUE) .'</pre>', array(), WATCHDOG_INFO, NULL);

You can watch the log in Reports -> Recent log messages (admin/reports/dblog) if the core module "Database logging" is activated.

Once your module is stable, you can remove the watchdog line.

  • It should be noted that $variables_to_log needs to be an array. I tried using a string and got an error. but $variables_to_log = array("foo", "bar", "hello", "world"); works fine. – Paul Trotter Jun 25 '14 at 10:49
  • I can't actually see the $variables_to_log variables in the log. Would I need to work them into t('My message')? – Paul Trotter Jun 25 '14 at 10:51
  • I edited my answer with more possibilities. In fact the $variable _to_lo must be an associative array of variable to replace in the message to log. – pbonnefoi Jun 25 '14 at 12:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.