开发者

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

开发者 https://www.devze.com 2023-04-10 06:42 出处:网络
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?

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. e.g.

/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.

0

精彩评论

暂无评论...
验证码 换一张
取 消