basecamp / kamal

Deploy web apps anywhere.
https://kamal-deploy.org
MIT License
9.38k stars 357 forks source link

Add grep options to log commands #817

Closed nickhammond closed 3 weeks ago

nickhammond commented 1 month ago

This adds in the ability to pass additional options to grep when utilizing the various logs commands(app, accessory, traefik).

This allows the user to pass in various options to grep such as -A 5 to look at the 5 lines after a match,-B 5 to look at the 5 lines before, or maybe count the number of matching lines with -c.

The use case is you're trying to monitor or look at a specific endpoint and you want to see the response codes for a bit. You know the endpoint is "/users/2fa" so you run kamal app logs -f --grep "users/2fa" --grep-options "-A 5" to provide 5 more lines of context after your grep. Since the primary use case at the moment for grep is to search for a request ID my thought was that we could utilize context to look at an entire request in the first grep potentially instead of needing to run it twice.

I also added in some missing tests for grep and broke up some of the tests that had multiple assertions within them to make it a bit easier to follow.

djmb commented 4 weeks ago

-C is not a valid flag for POSIX grep (though I'm sure it will be available the vast majority of the time), so I'm not sure we should encode it into Kamal.

How about adding a --grep-options flag and passing those verbatim?

So fir your example it would be kamal app logs -f --grep "users/2fa" -grep-options '-C 3'.

nickhammond commented 4 weeks ago

@djmb Ah, I didn't realize that. I like the --grep-options better though since I typically actually use -B or -A but didn't want to add two new options.

I'll get this PR adjusted!