Skip to content

Instantly share code, notes, and snippets.

@codeodor
Created March 21, 2019 23:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codeodor/8edfb7e1a2512e081824df6e688189df to your computer and use it in GitHub Desktop.
Save codeodor/8edfb7e1a2512e081824df6e688189df to your computer and use it in GitHub Desktop.
class DatabaseLogSubscriber < ActiveSupport::LogSubscriber
IGNORE_PAYLOAD_NAMES = ["SCHEMA", "EXPLAIN"]
def sql(event)
return unless logger.debug?
payload = event.payload
return if IGNORE_PAYLOAD_NAMES.include?(payload[:name])
conn_id = payload[:connection_id]
conn = ObjectSpace._id2ref(conn_id)
from_connection = conn.active? ? conn.current_database : "No active connection"
name = "#{payload[:name]} (#{event.duration.round(1)}ms) #{from_connection}"
name = "CACHE #{name}" if payload[:cached]
name = color(name, RED)
debug " #{name}"
end
end
DatabaseLogSubscriber.attach_to :active_record
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment