Grep Rails Log for Exceptions11 Sep 2018
I am working on a client project that uses USAePay as a a payment gateway. Unfortunately there is not much good I can say about the payment gateway and I am stuck with it. The app is old and uses an old version of ActiveMerchant, which in turn uses ActiveUtils. The timeout is set to 60s in ActiveUtils and the gateway may take about 45s to 10 minutes. The app generates 5 million lines of logs every day and I use grep to find things useful. A team member had interest in the regex I used and thus I thought it might be worth to share with you.
The idea is that a stack trace has its class name on top. Some thing like
ActionController::RoutingError. We can use this Error and what it precedes to find a a line number we are interested in for further inspection.
cat -n production.log-20180902 | grep -E "([[:digit:]]+)(\s+)((\w|:)+)Error"
Let me describe…
cat -nwill cat the file with line numbers
grep -Emeans for regular expression
[[:digit::]]is for any digit (shell script)
- The rest is usual regular expression
If you are not on Rails your regex would be different, but you get the idea.
This outputs some thing like this…
3560489 ActionController::RoutingError (No route ... 3560532 ActionController::RoutingError (No route ... 3560575 ActionController::RoutingError (No route ... 3560618 ActionController::RoutingError (No route ...