Skip to content
This repository has been archived by the owner on Dec 8, 2020. It is now read-only.

Commit

Permalink
Simplify ActiveRecord log subscriber so we dont have to deal with all…
Browse files Browse the repository at this point in the history
… of the changes
  • Loading branch information
binarylogic committed Dec 12, 2016
1 parent b8ec332 commit 17a1c95
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 50 deletions.
53 changes: 5 additions & 48 deletions lib/timber/probes/active_record_log_subscriber/log_subscriber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,64 +7,21 @@ class ActiveRecordLogSubscriber < Probe
# default behavior / log messages.
class LogSubscriber < ::ActiveRecord::LogSubscriber #:nodoc:
def sql(event)
return unless logger.debug?

self.class.runtime += event.duration
super(event)

payload = event.payload

return if defined?(IGNORE_PAYLOAD_NAMES) && IGNORE_PAYLOAD_NAMES.include?(payload[:name])

name = "#{payload[:name]} (#{event.duration.round(1)}ms)"
sql = payload[:sql]
binds = nil

unless (payload[:binds] || []).empty?
binds = " " + payload[:binds].map { |attr| render_bind(attr) }.inspect
end

name = colorize_payload_name(name, payload[:name])
sql = color(sql, sql_color(sql), true)

message = " #{name} #{sql}#{binds}"

event = Events::SQLQuery.new(
sql: payload[:sql],
time_ms: event.duration,
message: message
message: @message
)

debug event
logger.debug event
end

private
def colorize_payload_name(name, payload_name)
if payload_name.blank? || payload_name == "SQL" # SQL vs Model Load/Exists
color(name, MAGENTA, true)
else
color(name, CYAN, true)
end
end

def sql_color(sql)
case sql
when /\A\s*rollback/mi
RED
when /select .*for update/mi, /\A\s*lock/mi
WHITE
when /\A\s*select/i
BLUE
when /\A\s*insert/i
GREEN
when /\A\s*update/i
YELLOW
when /\A\s*delete/i
RED
when /transaction\s*\Z/i
CYAN
else
MAGENTA
end
def debug(message)
@message = message
end
end
end
Expand Down
7 changes: 5 additions & 2 deletions spec/timber/probes/active_record_log_subscriber_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,16 @@
logger.level = old_level
end

it "should log the controller call event" do
it "should log the sql query" do
User.order("users.id DESC").all.collect # collect kicks the sql because it is lazily executed
message = " \e[1m\e[36mUser Load (0.0ms)\e[0m \e[1m\e[34mSELECT \"users\".* FROM \"users\" ORDER BY users.id DESC\e[0m @timber.io {\"level\":\"debug\",\"dt\":\"2016-09-01T12:00:00.000000Z\",\"event\":{\"sql_query\":{\"sql\":\"SELECT \\\"users\\\".* FROM \\\"users\\\" ORDER BY users.id DESC\",\"time_ms\":0.0}}}\n"
# Rails 4.X adds random spaces :/
string = io.string.gsub(" ORDER BY", " ORDER BY")
string = string.gsub(" ORDER BY", " ORDER BY")
expect(string).to eq(message)
expect(string).to include("users.id DESC")
expect(string).to include("@timber.io")
expect(string).to include("\"level\":\"debug\"")
expect(string).to include("\"sql\":")
end
end
end
Expand Down

0 comments on commit 17a1c95

Please sign in to comment.