Hi,

Until I discovered Drush, I had a special Perl script for following the watchdog table in realtime. Since I discovered Drush and found it already had some watchdog support, I decided to have a go at porting this support into Drush itself for the good of all!

I've attached my patch to Drush to implement a "drush watchdog tail" command, with filter support and a --back parameter that works like tail's -n (how many previous rows to show).

It's a little bit ugly (calls out to the show() function with some magic), but it gets the job done. If you have any ideas for refactoring I'm happy to have another crack at it.

Tested on my Postgres setup. Someone'll need to make sure it works for MySQL, but should be fine.

Thanks

Comments

neilnz’s picture

StatusFileSize
new7.67 KB

Sorry fixed a typo in the examples section.

moshe weitzman’s picture

Anyone available to test and review code?

jonhattan’s picture

I tested with success.

EDIT: I suggest to catch CTRL+C (or other) and finalize the code run by normal way instead of aborting execution. It will present problems to #543550: Drush interactive mode :)

neilnz’s picture

Sure, that sounds like a good idea. Does Drush provide any facilities to handle a ctrl-c signal, or should I do it manually? Would it be better to use some alternative key combo, as ctrl-c behaviour should probably be preserved? I presume you'd like it to drop back to the shell upon aborting?

Is there an ETA on merging in this shell functionality, or should I build something that doesn't depend on it?

Sounds like a great idea by the way :)

jonhattan’s picture

neilnz: Yes, what I would like is to return to the shell upon `watchdog tail` abortion. I have no preference for ctrl-c or just `q` or another. I think drush does nothing with catching ctrl-c, and I've not thunk about the possibility of catching ctrl-c from the interactive shell for aborting a command.

Btw, there's no ETA for the interactive mode. I just code some lines time by time. Still in a kind of 'proof of concept' phase.

moshe weitzman’s picture

Status: Needs review » Needs work

This is pretty nifty. I'm not too versed with catching CTRL-C and the like. I suppose thats only a nice to have at this point.

Patch no longer applies. Would be great to get a reroll.

owen barton’s picture

Looks like you need to add a handler to catch SIGINT with http://php.oregonstate.edu/manual/en/function.pcntl-signal.php

I think this could probably be installed globally (perhaps trigger a drush hook, similar to hook_shutdown) - I guess this could be used to rollback any changes (e.g. half completed updates). Anyone want to take a shot?

That said, I am not sure I see any issue with just killing php/drush in the middle of tailing watchdog - were there some specific concerns here?

adrian’s picture

pcntl is not available on most binary distributions of php

neilnz’s picture

Are you sure this applies to the CLI binaries? I know on Debian/Ubuntu at least that pcntl is available in php-cli but not (F)CGI or Apache versions of the binary... So on the CLI it should work fine.

At any rate, we could attempt to load it up with dl() (if it's an extension?) and/or just fail to register the hook if it's not available (using function_exists() or the like). Much like we can fallback to not using readline/curses stuff if it's not there...

As for rerolling my patch, I'm overseas at the moment and not handy to my coding machine, so I'll get on to it when I get back (early Nov), unless someone else wants to have a crack at it first.

moshe weitzman’s picture

Status: Needs work » Closed (works as designed)

looked at this a bit and then discovered the 'watch' command in unix. i has to get it via macports.

`watch -n1 "drush ws"` is working for me.

neilnz’s picture

Status: Closed (works as designed) » Needs review
StatusFileSize
new5.22 KB

Hi again,

Sorry your last update prompted me to finally refactor my patch against the latest Drush CVS.

Using 'watch' is not a perfect solution, as it doesn't behave quite like tail does. The main problem is if more than 10 (or whatever selected limit for watchdog-show) messages are logged to the watchdog in between watch cycles, any beyond that limit are missed. A proper tail implementation won't miss anything from when it's started. It also removes the need to start and stop Drush (bootstrapping can take a while on a big Drupal instance).

So here's the patch, against CVS head. Please re-review when you have time.

By the way, theme('username') in core_watchdog_format_row() is passing an array cast of $watchdog as the parameter, but theme_username actually expects an object. To make it work for me on D6, I had to remove the array cast, but I didn't feel that should be part of this patch.

Thanks

anarcat’s picture

Status: Needs review » Needs work

Very nice work, I like this. I have only one issue with the way this works: the watchdog history is backward.

 Date              Severi  Type  Message                        User      
                   ty                                                     
 2010-01-30 11:45  notice  cron  Cron run completed.            Anonymous 
 2010-01-30 10:39  notice  cron  Cron run completed.            Anonymous 
 2010-01-30 09:35  notice  cron  Cron run completed.            Anonymous 
 2010-01-30 07:27  notice  cron  Cron run completed.            Anonymous 
 2010-01-30 06:23  notice  cron  Cron run completed.            Anonymous 
 2010-01-30 12:27  notice  user  Session closed for anarcat.  Anonymous 
                                                                        
 2010-01-30 12:27  notice  user  Session opened for anarcat.  Anonymous 
                                                                        

Column alignment issues aside (that's not your fault), I think that the order should be the exact opposite: more recent events should be at the bottom of the log, not at top. Notice how the "session" stuff looks out of order: the reason it's like this is because those entries were added after the tail was started, so those are in proper order.

It also seem like there's an extra newline on the stuff that appears after tail has been started.

@moshe - this is really not like "watch": watch will fire up drush every second and hit the whole bootstrap code everytime, very inefficient: the above patch will keep drush running indefinitely, using only limited resources (an SQL query from time to time and resident memory).

neilnz’s picture

Thanks for the feedback.

As for the history being backwards, I was wondering why things seemed a bit out of order. I think the order must have changed from wid ASC to wid DESC since my patch against an older version of Drush? Personally I don't know why you'd want to have it ordered in that direction, even for a single-shot watchdog-show command. I'm happy to redo my patch to flip around the order for just watchdog-tail or as the default for watchdog-show as well. What would be preferable?

As for column alignment, yes I agree. I would also like to see it dynamically calculate the terminal width on start and lay out accordingly, since at the moment many watchdog messages truncate just before the useful part of the message! I would like to be able to widen my terminal and see more of the message, and have the other columns fixed-width. I could have a look at implementing this in another patch if you like, but I imagine there's cross-platform issues to consider when getting the terminal width.

As for the extra newline, I'm not getting that, and I can't see anything in the code that would add a newline. I'm omitting the output of the header row, but otherwise I'm not adding another newline. I notice your column header for "severity" is also wrapping, could it be either your console program wrapping or something in the PEAR table library doing it?

anarcat’s picture

The messed up columns may be my usage of drush which involves bypassing the bash script and running drush.php directly. At least I *guess* that is the issue. (In my opinion, the columns detection stuff should be done in PHP, but then this is not the place to discuss that.)

I don't know if DESC/ASC changed recently, however i feel it's necessary for the SQL request to be ASC for the LIMIT to work the way it is now. I haven't crunched enough cycles to figure out a better way so I didn't improve your patch, but that's the key trick, I think.

neilnz’s picture

Status: Needs work » Needs review
StatusFileSize
new5.57 KB

I think you mean DESC (which it is), and you're probably right, it would be necessary to use negative offsets otherwise for it to select the right rows.

I guess the easiest way to correct this is to use an array_reverse($rows) after the rows are built, but before the header row is put on the top.

I've attached a new patch that does it this way.

jonhattan’s picture

I'd like to make this issue dependant on #170583: watchdog fixes and improvements as both work on the same code and #170583 is a major refactor that will allow a cleaner patch for wd-tail.

moshe weitzman’s picture

#170583: watchdog fixes and improvements has landed. feel fee to proceed here.

jonhattan’s picture

StatusFileSize
new4.6 KB

Patch attached.

* wd-show remains the same: recent events at the top.
* wd-tail: recent events at the bottom.
* uses a trick around Console_Table for the display.
* Ctrl-C to abort (no catch).

greg.1.anderson’s picture

Status: Needs review » Reviewed & tested by the community

Works well; I like it.

I'll mention the two thoughts I did have. 'watchdog-tail' could also just be 'watchdog-show --tail'; fewer commands that way, but I don't mind it the way it is, either.

It also might be nice to be able to specify the value to pass to sleep via an option, to give more control over load vs. responsiveness. Since 'tail' is less likely to be run on a production site, load probably isn't much of an issue, though, so I think this could be committed as-is.

jonhattan’s picture

StatusFileSize
new3.78 KB
new3.71 KB

I also like 'watchdog-show --tail'. Changed in the first patch attached.

Also attached an alternative patch that allows for --tail=3 and this is the argument for sleep(); if --tail the value is 1. I'm not specially interested in this, just to play.

greg.1.anderson’s picture

--tail=3 isn't a good option; people who want to set it will likely put $options['sleep-delay'] = 3; in a config file. If you overload the sleep delay to also mean 'tail', then you can't do this without disabling watchdog-show.

jonhattan’s picture

StatusFileSize
new4.5 KB

Here is a patch with support for sleep-delay. I've also added some comments to the code. Feel free to correct my poor english in the options description.

There's also an error with `theme('username' (array)$result)` I will address in a new issue.

moshe weitzman’s picture

Status: Reviewed & tested by the community » Fixed

committed. thanks.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.