How can I track statistics on how often a hook/error message is triggered? I am using the Privatemsg module in Drupal 7.  I have a hook in a custom module that is triggered when users write a message that is too short.  I set this up using the instructions here.

Basically, it looks like this:

function mymodule_privatemsg_message_validate($message) {
  $uid = $vars['message']-&gt;author-&gt;uid;

  //if root or help user, message should always go through regardless of length
  if ($uid !== '1') {
    if ($uid !== '3') {

      if (empty($message-&gt;thread_id) &amp;&amp; strlen($message-&gt;body) &lt; 200 ) {
    form_set_error('[body][value]', t('My custom message.'));
      }
    }
  }
}


I recently noticed there are not so many messages being sent on my site.  I would like to know if it is because they are being blocked by this hook.  How can I track how many times it is triggered (and even better if I can know which logged-in users are getting the error message)?
I don't think you'll get 'statistics' as such without some coding yourself, I've never seen a module that does that sort of thing (but hey it's Drupal so there probably is one somewhere).

Failing that I think your best bet would be to use the the watchdog() function, combined with enabling the dblog module on your site. You can use it like so:

if (empty($message-&gt;thread_id) &amp;&amp; strlen($message-&gt;body) &lt; 200 ) {
  form_set_error('[body][value]', t('My custom message.'));

  global $user;
  watchdog('mymodule', 'My custom message for user @username', array('@username' =&gt; $user-&gt;name));
}


Every time that if statement is entered you'll get a message on the 'Recent log messages' page, where you can even filter by entries for your own module, giving you a basic idea of what's happening, when, and for whom.