将 Ruby on Rails 中的 500/404 错误保存到数据库?

发布于 2024-12-07 03:41:36 字数 255 浏览 0 评论 0原文

有没有办法将 500/404 等错误保存到数据库中,以便您可以检查它们以查看网站上是否存在任何错误?

我以为你可以从 500.html 页面发送 JS AJAX 请求。例如

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

,但我不确定在生产模式下运行时如何获取该信息?

非常感谢任何帮助,

亚历克斯

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小…红帽 2024-12-14 03:41:36

这就是我的应用程序控制器中的内容:

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

如您所见,我创建了一个错误模型,每当发生错误时都会将一个新模型保存到数据库中。要记住的一件事是,回溯对于字符串列来说太大了,因此您需要更大的东西,例如文本类型。这适用于我的 Rails 3.0.5 应用程序。

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.

臻嫒无言 2024-12-14 03:41:36

不建议将错误记录到数据库,因为这些错误通常可能是由数据库问题引起的。如果您的站点流量很高,则将错误附加到文件(在单独的磁盘上)会更安全,如果文件系统没有响应,那么您的数据库将无法工作。更安全的是使用托管在另一台服务器上的异步消息队列。在这两种情况下,您都可以通过定期解析日志输出来创建报告。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文