title: Redirect logger output for a specific controller in Rails 3
body: <p>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 did this by redefining the "logger" method but in Rails 3, the way to log is using "Rails.logger". I tried putting </p>
  
  <pre><code>Rails::logger = Logger.new(File.join(Rails.root, 'log', "reports_controller.log"), 10, 1000000) 
  </code></pre>
  
  <p>at the top of the controller, but only the specific cases where Rails.logger is used specifically in the action is getting sent to the specified log file, all of the default log output for the controller is still routing to the main log file. </p>
  
  <p>How can we route all log output for a specific controller to a specific log file to include all default controller output?</p>
  
  <p>By default controller output, I am referring to all of the messages that start with the very beginning of the inbound request </p>
  
  <pre><code>Started POST "/api/v1/reports.json" for 10.XXX.XX.XX at 2015-03-07 01:30:22 +0000
  Processing by Api::V1::ReportsController#create as JSON
    Parameters: {"report"=&gt;{"cloud_file_path"=&gt;"report.zip", "company_id"=&gt;nil, "created_at"=&gt;"2015-03-07T01:30:17Z", "type_id"=&gt;"2", "updated_at"=&gt;"2015-03-07T01:30:17Z", "master"=&gt;"1"}}
  </code></pre>
  
  <p>and all log output that might follow from the inbound request and outbound response in the controller, etc.</p>
  
  <p>Basically I want all logging for the reports controller to be in the reports_controller.log and I don't want any messages for traffic to the reports controller to show up in the main log (i.e. production.log if in production)</p>
  
  <p><strong>Update:</strong> </p>
  
  <p>Thanks to @mudasobwa's help with his answer and chat, I was able to solve this using middleware as his answer depicts (although I had to change my insert_before to be before Rails::Rack::Logger)</p>
  
  <p>the revised answer from him that solved it for me is below and it lives in <code>config/initializers/logger_middleware.rb</code></p>
  
  <pre><code>module MyApp
    class LoggerMiddleware
  
      REPORTS_API_CONTROLLER_PATH = %r|\A/api/v.*/reports/.*|
      REPORTS_API_CONTROLLER_LOGFILE = "reports_controller.log"
  
      def initialize(app)
        @app, @logger = app, Rails::logger.instance_variable_get(:@logger).instance_variable_get(:@log)
        @reports_api_controller_logger = Logger.new(Rails.root.join('log', REPORTS_API_CONTROLLER_LOGFILE), 10, 1000000)
      end
  
      def call(env)
        Rails::logger
             .instance_variable_get(:@logger)
             .instance_variable_set(:@log,
                 case env['PATH_INFO']
                 when REPORTS_API_CONTROLLER_PATH then
                   @reports_api_controller_logger
                 else
                   @logger
                 end
             )
        @app.call(env)
      end
    end
  end
  
  Rails.application.middleware.insert_before Rails::Rack::Logger, MyApp::LoggerMiddleware
  </code></pre>