Reputation: 523
To debug possible issues with some new method, I'd like to log the stack trace upon encountering a StandardError.
In my case the stack traces are extremely long and only the last few calls are of importance to me (to debug a possible issue, I need to know who was the caller).
Is there a way to log a truncated version of the stack trace association with the exception?
E.g.
def some_unreliable_method
begin
# do unreliable things
rescue => e
log(
error_message: e.message,
backtrace: e.backtrace # the full stack trace is too long
)
# recover from exception
end
end
Upvotes: 2
Views: 557
Reputation: 5204
e.backtrace
is just array and you can get only last N numbers of backtrace's lines:
# get last 10 lines
e.backtrace[0, 10].join("\n\t")
Upvotes: 4
Reputation: 8888
Instead of log(error_message: e.message, backtrace: e.backtrace)
, just logger.error(e)
If that's still too long, take a look at config/initializers/backtrace_silencers.rb
. It's well documented.
Upvotes: 0