0
class ApplicationController < ActionController::Base    
  before_action :init_logger

  def home
    logger.info 'test'
  end

  def init_logger
    logger = MyLogger.new(env)
    Rails.logger = logger
  end
end

This does not work as expected because the logger in the home action is not the same logger in the before action. The logger in the home action is the default Rails logger.

How to set the Rails logger in the before action?

B Seven
  • 44,484
  • 66
  • 240
  • 385

1 Answers1

0

If you are trying to do just for Application controller

then try the following

def logger
        @logger
end

def init_logger
    @logger = MyLogger.new(env)
 end

If you want to do for all then you need to initialize in environment.rb file

How to get Rails.logger printing to the console/stdout when running rspec?

Community
  • 1
  • 1
Keval
  • 3,246
  • 20
  • 28