From bec65fa261b9803c19edbbe9dc89836e1edf78c7 Mon Sep 17 00:00:00 2001 From: Peter Boling Date: Fri, 17 Jul 2015 13:23:59 -0700 Subject: Improve sql logging coloration in `ActiveRecord::LogSubscriber`. - Improves coloring for statements like: # Become WHITE SELECT * FROM ( SELECT * FROM mytable FOR UPDATE ) ss WHERE col1 = 5; LOCK TABLE table_name IN ACCESS EXCLUSIVE MODE; # Becomes RED ROLLBACK - Reinstates the coloration of the `payload[:name]`. Instead of simple alternating colors, adds meaning: - `MAGENTA` for `"SQL"` or `blank?` payload names - `CYAN` for Model Load/Exists - Introduces specs for sql coloration. - Introduces specs for payload name coloration. GH#20885 --- activerecord/lib/active_record/log_subscriber.rb | 28 ++++++++++++++++++------ 1 file changed, 21 insertions(+), 7 deletions(-) (limited to 'activerecord/lib/active_record/log_subscriber.rb') diff --git a/activerecord/lib/active_record/log_subscriber.rb b/activerecord/lib/active_record/log_subscriber.rb index 4d597a0ab1..9e3f1bfb9e 100644 --- a/activerecord/lib/active_record/log_subscriber.rb +++ b/activerecord/lib/active_record/log_subscriber.rb @@ -47,7 +47,11 @@ module ActiveRecord binds = " " + payload[:binds].map { |attr| render_bind(attr) }.inspect end - name = color(name, nil, true) + if payload[:name].blank? || payload[:name] == "SQL" # SQL vs Model Load/Exists + name = color(name, MAGENTA, true) + else + name = color(name, CYAN, true) + end sql = color(sql, sql_color(sql), true) debug " #{name} #{sql}#{binds}" @@ -55,12 +59,22 @@ module ActiveRecord def sql_color(sql) case sql - when /\s*\Ainsert/i then GREEN - when /\s*\Aselect/i then BLUE - when /\s*\Aupdate/i then YELLOW - when /\s*\Adelete/i then RED - when /transaction\s*\Z/i then CYAN - else MAGENTA + when /\A\s*rollback/mi then + RED + when /\s*.*?select .*for update/mi, /\A\s*lock/mi then + WHITE + when /\A\s*select/i then + BLUE + when /\A\s*insert/i then + GREEN + when /\A\s*update/i then + YELLOW + when /\A\s*delete/i then + RED + when /transaction\s*\Z/i then + CYAN + else + MAGENTA end end -- cgit v1.2.3