1

I have a custom module that performs an import from an xml file. It can be invoked two different ways. From a menu callback or a custom drush command. The import works fine and all of it's many watchdog() calls work fine when I execute it from the menu callback. But if I execute if from Drush, the import works, but not watchdog entries are logged.

Why is this? How can I get watchdog to work in this module when it's triggered from Drush?

  • drush, by default, executes as the anonymous user unless you give it the -u option. Does your menu callback call the watchdog as anon? What happens when you use the -u option to call your function? – mpdonadio Jul 22 '14 at 22:11
0

Can you post your hook_drush_command?

Reading briefly from here I will venture to guess you have to call the watchdog function inside this hook as well (after your import statements).

  • function bunn_prod_manual_import_drush_help($command) { switch ($command) { case 'drush:prod-manual-importing': return dt('Import Product Manuals'); } } function bunn_prod_manual_import_drush_command() { $items = array(); $items['prod-manual-import'] = array( 'description' => "Import product manuals.", 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL, ); return $items; } – Chris Desautels Jul 23 '14 at 19:45
0

This is working for me, using the latest Drush dev-master:

drush @dev ev 'watchdog("system", "test watchdog message.");'

What I am doing here is just calling the 'watchdog' function directly with the php-eval command, after bootstrapping a dev site. This should produce the same results as a watchdog function called from a module, and in fact I see the message logged in the Drupal admin/reports/dblog page.

Drush does install a watchdog handler, so that it can log the watchdog messages to the console in addition to the dblog page; however, many of these messages are ignored by Drush as uninteresting. To see the full range of watchdog messages that Drush sees, set the --watchdog flag to 'print':

drush @dev --watchdog=print ev 'watchdog("system", "another watchdog message.");'

You should see this message printed in the console, and it should also appear in the dblog page.

I'm not sure why you are not seeing anything in your dblog page, but you might try upgrading Drush (if you're running an old version), and try some of these commands and see what results you get. This should give you some indication of where the problem is, at least.

UPDATE:

It seems impossible that the Drush hook should be called, but that nothing should be added to the dblog. The watchdog function in core does nothing but call hooks:

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

The actual database write happens in function dblog_watchdog(), which is in dblog.module. It invariantly writes the entry to the database.

The only guess I have at this point in time is that perhaps you have multisite set up, and Drush is targeting the wrong site or database when you call your menu hook via a drush command. drush status might help determine if this is the case. Next step would be to put a breakpoint on dblog_watchdog(), or add some sort of print statement there and see what happens when you run your function from Drush.

  • I think you miss the point. I don't want to create a watchdog message from Drush. I want the watchdog invocations in my custom module to work when the custom module is invoked from Drush. BTW, the commands above didn't work in any case. I'm using Drush v.6 – Chris Desautels Jul 23 '14 at 20:03
  • The commands I entered above were just for testing; they should behave the same way that watchdog invocations in your custom module do. You confirmed this when you ran the same commands, and saw nothing in dblog. The next interesting test for you to try is to run your custom Drush command with --watchdog=print. If you see your watchdog messages printed to the console, then you know that the watchdog hooks are firing when you call your code. I'm still not quite sure why you would see different results from Drush. Check the first parameter to your watchdog call, maybe. – greg_1_anderson Jul 23 '14 at 20:12
  • I elevated the type of message watchdog is creating. It was just WATCHDOG_INFO, which apparently Drush ignores. By making them WARNING and ERROR, they now print to the console. But they still don't get saved to the Drupal log. – Chris Desautels Jul 23 '14 at 20:52
0

You could just pass the stdout from Drush as the stdin to grep.

Example:

drush wd-show | grep 'Undefined offset'

As a bonus - If you want the errors in a file, try the following:

drush wd-show | grep 'Undefined offset' > ~/Undefined-errors.txt

This'll put the errors into a text file in your home directory.

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.