Log to database instead of log files

前端 未结 6 1424
别跟我提以往
别跟我提以往 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:07

    If you want to change the default logging behavior, simply create a custom logger object that respond to all the Rails logger method:

    • add
    • debug, warn, error, info, fatal, unknown

    http://github.com/rails/rails/blob/9d7aae710384fb5f04129c35b86c5ea5fb9d83a9/activesupport/lib/active_support/buffered_logger.rb

    Because it's your logger, you can decide to implement your personal logic. You can write to the database, to the standard output of whenever you want.

    Then, replace the default logger for every base class you want to customize.

    ActiveRecord::Base.logger = YouLogger.new
    

    You can easily create an initializer file called logger.rb and write there all your custom configurations. In this way, the logger will be immediately replaced on Rails startup.

提交回复
热议问题