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
| Comment | File | Size | Author |
|---|---|---|---|
| #22 | 547902.patch | 4.5 KB | jonhattan |
| #20 | 547902.patch | 3.71 KB | jonhattan |
| #20 | 547902-sleep.patch | 3.78 KB | jonhattan |
| #18 | 547902.patch | 4.6 KB | jonhattan |
| #15 | watchdog.drush_.inc_.tail2_.patch | 5.57 KB | neilnz |
Comments
Comment #1
neilnz commentedSorry fixed a typo in the examples section.
Comment #2
moshe weitzman commentedAnyone available to test and review code?
Comment #3
jonhattanI 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 :)
Comment #4
neilnz commentedSure, 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 :)
Comment #5
jonhattanneilnz: 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.
Comment #6
moshe weitzman commentedThis 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.
Comment #7
owen barton commentedLooks 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?
Comment #8
adrian commentedpcntl is not available on most binary distributions of php
Comment #9
neilnz commentedAre 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.
Comment #10
moshe weitzman commentedlooked 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.
Comment #11
neilnz commentedHi 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
Comment #12
anarcat commentedVery nice work, I like this. I have only one issue with the way this works: the watchdog history is backward.
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).
Comment #13
neilnz commentedThanks 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?
Comment #14
anarcat commentedThe 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.
Comment #15
neilnz commentedI 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.
Comment #16
jonhattanI'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.
Comment #17
moshe weitzman commented#170583: watchdog fixes and improvements has landed. feel fee to proceed here.
Comment #18
jonhattanPatch 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).
Comment #19
greg.1.anderson commentedWorks 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.
Comment #20
jonhattanI 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.
Comment #21
greg.1.anderson commented--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.Comment #22
jonhattanHere 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.
Comment #23
moshe weitzman commentedcommitted. thanks.