Search code examples
ruby-on-railsloggingomniauth

Configure OmniAuth logger to two different loggers depending the level of the error


In my Rails 5 app I have 'omniauth', '~> 2.0.3' where in my config config/initializers/omniauth.rb I'm attaching the logger to the Rails.logger.

OmniAuth.config.logger = Rails.logger

What I want to do is if the log level is error I want to use Rollbar instead of the Rails logger.

Is there a way to do this?


Solution

  • you can create a proxy logger which delegate error level logs to Rollbar, other levels will delegate to Rails.logger

    class ProxyLogger
      delegate :error, to: :@error_logger
      delegate_missing_to :@default_logger
    
      def initialize(error_logger:, default_logger: Rails.logger)
        @default_logger = default_logger
        @error_logger = error_logger
      end
    end
    
    OmniAuth.config.logger = ProxyLogger.new(error_logger: Rollbar)