Log to database instead of log files

前端 未结 6 1440
别跟我提以往
别跟我提以往 2020-12-12 10:28

I\'m interested in sending all Rails application logging to a database (MySQL or MongoDB) either in addition to or instead of to a log file. There are a few reasons, most of

6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-12 11:08

    Having made the mistake of logging to a database recently myself, I feel I can offer one extremely good reason why you should not do this: Transactions. Let's say you start a transaction, log a bunch of stuff during the course of the transaction, and ultimate you end up with an error condition. You log the error condition, and oh hey. ROLLBACK. Suddenly, everything you just logged is gone and you have no idea what happened or why.

    And particularly in the context of Rails, where really useful libraries like AASM will wrap a whole bunch of stuff in a transaction, you can end up with transactions in places you didn't think you would, which also makes the problem very hard to debug.

    In my case, the reason I logged things to the database was that I needed context-sensitive logs. Essentially I needed to be able to look up all log entries related to a specific database model. However, the right answer is to put those logs in some separate location that's a better fit for log data (and which, in my case, happens to be query-able).

提交回复
热议问题