2

In my Rails app production mode,I set the error log level to error.

But I want to set some controllers an actions(or routes) to the level access,so how can I realize this? If the log has a filter?

HXH
  • 1,643
  • 4
  • 19
  • 31

1 Answers1

1

You can use an around filter to adjust the logging level for an action, For example:

class MyBigFancyController < ApplicationController
  around_action :adjust_logging_level_to_access, only: :show

  def show
  end

  private

  def adjust_logging_level_to_access
    old_level = Rails.logger.level 
    Rails.logger.level = Logger::DEBUG
    yield
    Rails.logger.level = old_level
  end
end

If you have to do this in many controllers, consider moving it to the ApplicationController.

Please note that adjusting the Rails logging level at run time is not thread safe. If you require thread safety, you will need to manually write to the log in the relevant locations.

Brad Werth
  • 17,411
  • 10
  • 63
  • 88
  • The linked http://stackoverflow.com/q/2196828/525478 has some interesting approaches, but you would have to flip them around... – Brad Werth Aug 27 '15 at 04:20
  • Thanks,this is a good solution.But I have a question,If two user visit the page A that has `adjust_logging_level_to_access` and the another page B without `adjust_logging_level_to_access` at the same time.So will page B logged DEBUG?Because the log level is globle, so change the level in page A will affect the log level in page B,isn't that? – HXH Aug 27 '15 at 05:12
  • @HXH Yes, if your server is multithreaded and running multiple threads. This is a current limitation of Rails, as discussed in github.com/rails/rails/issues/20490 – Brad Werth Aug 27 '15 at 06:53