Drupal – adding debug messages to your custom module

Note: There is a follow up post here: Drupal – adding debug messages to your custom module – Improved


I work with a few custom modules and lately I’ve been migrating one from PHP 5.2 to 5.3 & Webform 2 to 3.

It’s been a challenge to sort out where some of the the errors are being generated from so I added a grip of debugging statements. In fact, I used a regex replace to log the name of each function that’s run in hopes that I could track down errors in chronological order.

That worked OK but I though I may want to turn it off or switch from drupal_set_message() to watchdog() calls when it’s ready to go to production. So I wrapped each log item in an if statement and tested for two new module variables:

  • Enable debugging message feedback
  • Enable watchdog debugging feedback

So now I have some GUI control over how and when debug messages are used.
Screen Shot of the new checkboxes

Here’s the code I appended to the hook_admin form function

<?php
function mymodule_admin() {
  //...various form fields...

  // Debugging - fieldset for debug related fields
  $form&#91;'mymodule_debug'&#93; = array(
    '#type' => 'fieldset',
    '#title' => t('Debugging'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );

    $form['mymodule_debug']['mymodule_debug_message'] = array(
      '#type' => 'checkbox',
      '#title' => t('Enable debugging message feedback'),
      '#description' => t('Displays feedback in Drupal Messages'),
      '#default_value' => variable_get('mymodule_debug_message', FALSE),
      '#required' => FALSE
    );

    $form['mymodule_debug']['mymodule_debug_reporting'] = array(
      '#type' => 'checkbox',
      '#title' => t('Enable watchdog debugging feedback'),
      '#description' => t('Displays feedback messages in the Recent Log Entries (watchdog) Report'),
      '#default_value' => variable_get('mymodule_debug_reporting', FALSE),
      '#required' => FALSE
    );

  return system_settings_form( $form );

}// function mymodule_admin()

Then for each function I inserted code like this:

<?php
function mymodule_help($path, $arg) {
  if( variable_get('mymodule_debug_message', FALSE) ){
    drupal_set_message('Running function mymodule_help');
  }
  if( variable_get('mymodule_debug_reporting', FALSE) ) {
    watchdog('mymodule','Running function mymodule_help');
  }
&#91;/code&#93;


Here's the regex I used in the find & replace:
(Dreamweaver regex)
Find:
<pre>function (.*)((.*))[s]*{</pre>
Replace:
function $1($2) {
  if( variable_get('mymodule_debug_message', FALSE) ){
    drupal_set_message('Running function $1');
  }
  if( variable_get('mymodule_debug_reporting', FALSE) ) {
    watchdog('mymodule','Running function $1');
  }

Now, for a short time my log is filled with lots of nearly useless logging! The idea being that it need not be useless for long.

Here's a quick look at my watchdog log gui with both types of error reporting enabled:
Screen Shot of watchdog screen

Leave a Comment