问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/9773259/rails-logging-server-errors-to-db