Hooking into ActiveRecord logging

Quick and dirty hook to log long SQL queries and their caller.

ActiveSupport::Notifications.subscribe 'sql.active_record' do |name, start, finish, id, payload|
  time = (finish - start) * 1000
  if payload[:sql] && time > 500
    time = (time * 1000).to_i / 1000.0
    c = caller.select { |l| l =~ /something_(core|music|artists)/ }[0]
    logger = Logger.new(Rails.root.join('log', 'active_record_slow_queries.log'))
    logger.info("(#{time}s) #{payload[:sql]} - #{c}")
  end
end