简体   繁体   中英

Saving 500/404 errors in Ruby on Rails to a database?

Is there a way to save the 500/404 etc errors to your database so you can check them to see if there are any bugs on the site?

I thought you could send an JS AJAX request from the 500.html page. eg

/errors/create/?message=error_message&ip=123&browser=ie9

But I'm not sure how to get that information when running in production mode?

Any help greatly appreciated,

Alex

This is what I have in my application controller:

def rescue_action_in_public(exception)
  #This is called every time a non-local error is thrown.
  #Copy the error to the db for later analysis.
  Error.create :exception_name => exception.exception.to_s, :backtrace_info => exception.backtrace.to_s
  #Then handle the error as usual:
  super
end

As you can see I have an Error model I created and this saves a new one to the DB whenever it happens. One thing to remember is that backtraces are much to big for string columns so you will need something bigger like a text type. This works in my Rails 3.0.5 app.

Logging errors to the db is inadvisable since these errors can often be caused by database issues. It's safer to append your errors to a file (on a separate disk) if your site is high traffic, if the file system is unresponsive, then your db won't work anyway. Even safer would be to use an asynchronous message queue hosted on another server. In both cases, you can create reports by periodically parsing your log output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM