Capturing warnings in an exception tracker

I sought to enable ActiveRecord.error_on_ignored_order so that we could use the backtrace to understand which scopes are having their order ignored when we call an ActiveRecord::Batches method on them. Rails’ default behaviour here is to log a warning and proceed – to raise an error instead would stop execution, but it would give us the backtrace we need to track down the offending relation. The ideal middle ground would be to send an error to error tracking software so that we have the backtrace too, but stopping execution might not always be appropriate in the case of a warning

To log an error and proceed, a monkey patch is necessary:

ActiveSupport.on_load(:active_record) do
  module ActiveRecord::Batches
    private

    def act_on_ignored_order(error_on_ignore)
      raise_error = (error_on_ignore.nil? ? ActiveRecord.error_on_ignored_order : error_on_ignore)

      error_to_raise = ArgumentError.new(ORDER_IGNORE_MESSAGE)
      if raise_error
        raise error_to_raise
      else
        # Tell your error reporting software about it (e.g. Airbrake)
      end
    end
  end
end

It would have been good for Rails to natively support error tracking software for this circumstance and provide it as an option over raising an error. I think it might be possible that there are other warnings we would like to know about and track similarly. Enabling error_on_ignored_order was good for finding instances that were covered by the test suite, but there may be more in production.

It would be possible for us to create a custom logger that sends all warnings to error tracking software in this way, but that might be too broad. Could Rails provide a way to natively send some warnings to error tracking software?