Rails-将服务器错误记录到数据库 [英] Rails - logging server errors to DB

查看:55
本文介绍了Rails-将服务器错误记录到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在服务器日志中发现500个错误,我希望将它们存储在表中,例如server_errors(id,user_id,created_at,build_version,error_code,error_output)中.

我最初的想法是只运行一个脚本,该脚本会不断捕获log/production.log中的错误并将它们输入时存储到数据库中,但这似乎是一个丑陋的解决方案,当您考虑诸如日志轮换之类的操作时会变得笨拙等

这似乎是一个足够普遍的功能,应该为此提供一个宝石,或者Rails(2.3.12)内置的一些标准功能,因此我希望有人可能知道类似的东西.或者,如果您可以想到一种方法,则可以建议一种更好的方法.

解决方案

大多数人使用 exception_notification NewRelic RPM ,或 AirBrake (以前称为Hoptoad)查看其例外情况.

如果您想自己动手,请看看如何.它使用中间件组件来捕获异常.您可以在自己的自定义中间件中捕获异常,然后将信息保存到上述数据库表中,而不用通过电子邮件发送.

对于后台队列,请查看resque的执行方式.

如果您使用的是Rails 2.3,则应使用 2-3-稳定的exception_notification分支,而不是主分支.

If I get 500 errors in my server logs, I would prefer to store them to the DB in a table like server_errors (id, user_id, created_at, build_version, error_code, error_output).

My initial idea is to just run a script that constantly greps log/production.log for errors and stores them to the DB as they come in, but this seems like an ugly solution and gets unwieldy when you consider things like log rotation etc.

This seems like a common enough feature that there should be a gem for it, or some standard functionality built into Rails (2.3.12) so I was hoping that somebody might know of something like that. Or could suggest a better methodology if you can think of one.

解决方案

Most folks use exception_notification, NewRelic RPM, or AirBrake (formerly known as Hoptoad) to see their exceptions.

If you would like to roll your own, take a look at how exception_notification does it. It uses a middleware component to catch the exceptions. You could catch the exceptions in your own custom middleware, then save the information to the database table you described above instead of emailing it.

For background queueing, see how resque does it.

If you're using Rails 2.3, you should use the 2-3-stable branch of exception_notification instead of the master branch.

这篇关于Rails-将服务器错误记录到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆