Redirect logger output for a specific controller in Rails 3

后端 未结 3 1030
日久生厌
日久生厌 2021-02-09 14:40

We want to have a collection of controllers where we route logger output from all actions and downstream methods to a separate log file. This is a Rails 3 project. In Rails 2 we

3条回答
  •  灰色年华
    2021-02-09 15:10

    Have you tried prepending an around_filter?

    class MyController < ApplicationController
      prepend_around_filter :set_logger
    
      private
    
      def set_logger
        old_logger = Rails::logger
        Rails::logger = Logger.new(Rails.root.join('log', "reports_controller.log"), 10, 1000000) 
        yield
        Rails.logger = old_logger
      end
    end
    

提交回复
热议问题