Drush provides system_watchdog() as a sneaky implementation of hook_watchdog(). This is available for D6 and D7. D5 doesn't provide that hook.
I have found it is not working as expected and I've done some tests. Don't know if it's a weird issue in my setup, something affecting D6 or something in front of my eyes that I'm unable to see.
For my tests I've added print "call to function system_watchdog()\n"; in system_watchdog() and also print "This module implements hook_watchdog: $module\n"; in watchdog() function (includes/bootstrap.inc:845).
1- register a INFO message in the watchdog. No output. that's ok.
$ drush php-eval "watchdog('drush test', 'qwerty', array(), WATCHDOG_INFO)"
This module implements hook_watchdog: dblog
This module implements hook_watchdog: system
call to function system_watchdog()
2- register a ERROR message in the watchdog. Drush prints the message as expected.
$ drush php-eval "watchdog('drush test', 'qwerty', array(), WATCHDOG_ERROR)"
This module implements hook_watchdog: dblog
This module implements hook_watchdog: system
call to function system_watchdog()
WD drush test: qwerty [error]
3- Same tests with --verbose. No module implements hook_watchdog!
$ drush --verbose php-eval "watchdog('drush test', 'qwerty', array(), WATCHDOG_INFO)"
Initialized Drupal 6.15 root directory at /var/www/drupal-6.x-cvs [notice]
Initialized Drupal site d6 at sites/d6 [notice]
Command dispatch complete [notice]
$ drush --verbose php-eval "watchdog('drush test', 'qwerty', array(), WATCHDOG_ERROR)"
Initialized Drupal 6.15 root directory at /var/www/drupal-6.x-cvs [notice]
Initialized Drupal site d6 at sites/d6 [notice]
Command dispatch complete [notice]
same with --debug.
In drupal7 it works but .... module_implements() uses a cache in the database. If you clear cache from drush and run the tests, it works. But if cache is rebuilt from the web interface .. it won't find our implementation of hook_watchdog().
so we don't have wd messages for D5, there's some weirdness in D6 and it is not reliable for D7.
I think of two possible solutions:
a) use a external module (devel) to implement hook_watchdog() and pass messages to drush.
b) implement other mechanism in drush (more complex and expensive): get last wid at the begin and end of a drush execution. If they are distinct, based on the verbosity level, do a select on watchdog table. It requires dblog module enabled and is also a solution for D5.
Comments
Comment #1
moshe weitzman commentedNice detective work. Indeed, core drupal is not helping us out here.
3) The issue is that watchdog() gets called early in the FULL bootstrap phase, before system module is included. At that point we cache all found implementations of hook_watchdog() and it doesn't get refreshed even when new modules are loaded. Pretty bad bug IMO, but not fixable in a stable release. I've added code in drush's FULL bootstrap so that we clear that cache. This example now works.
For D7, the same sort of trick works. We have to clear the implements cache ourselves.
Committed one line fix.
Comment #2
jonhattanFor the record, this fix in drupal-6 cvs make all tests work:
http://drupalcode.org/viewvc/drupal/drupal/includes/common.inc?r1=1.756....