lograge
lograge copied to clipboard
exception_object in not present in payload
Hi, I am trying following from the Readme to have exception backtrace:
YourApp::Application.configure do
config.lograge.enabled = true
config.lograge.custom_options = lambda do |event|
{
exception: event.payload[:exception], # ["ExceptionClass", "the message"]
exception_object: event.payload[:exception_object] # the exception instance
}
end
end
But once an error is raised exception_object
is not present in a payload resulting to Could not log "process_action.action_controller" event. NoMethodError: undefined method "backtrace" for nil:NilClass
.
Why it is not there and how I can get it?
@mkarganov Did you find a solution for this? I am also facing the same issue. payload[:exception_object] is nil for me.
@vik-y @mkarganov Where are the exceptions thrown? Do you have a stack trace?
No, I have not solved it. I log errors now within a custom logger which has access to exception by handling them via rescue.
@pxlpnk I would not try again, but if you will try the part from documentation that I posted and try to raise an exception via normal flow - you will find out that the proposed code does not work as event
object is nil
Exactly. I tried to test it by making a syntax error in my views and using the code provided in the documentation. I did get correct error message in payload[:exception]
but payload[:exception_object]
was nil.
It shows up for me in a subset of events. When it does it doesn't have any backtrace... Rails 5.0 here.
I am also facing this issue e.g inspection the payload I can see
:exception=>["ActiveRecord::RecordNotFound", "Couldn't find Upload with 'id'=318375"]
:exception_object is not set.
looks like lib/lograge/log_subscribers/base.rb might be the place to fix it:
def extract_status(payload)
if (status = payload[:status])
{ status: status.to_i }
elsif (error = payload[:exception])
exception, message = error
{ status: get_error_status_code(exception), error: "#{exception}: #{message}" }
else
{ status: default_status }
end
end
This also explains a weird discrepancy that I had somehow worked around some time ago, I have a
config.lograge.formatter = lambda do |data| ...
...block, in which I reference data[:error] to get the exception