Skip to content

Commit f5d8dd6

Browse files
committed
More granular console SQL coloration
This new coloration approach makes it easier to scan the rails console for specific types of activity with more fine-grained visual cues. Virtual terminal ANSI color escape codes are used when displaying SQL statements in the rails console. The former implementation alternates line prefix information (including the statement name and execution latency) between CYAN and MAGENTA. This visually differentiates any SQL statements in the log and is useful for quickly scanning for database activity. While a great idea and a solid foundation, alternating between just two colors on an even/odd basis (much like striping an HTML table) can be improved upon. This patch replaces the even/odd striping with a more comprehensive scheme that applies coloration based on the type of statement being run. Every statement logged has its prefix (name and latency) colored white (as the statement body was previously). The statement body is now colored according to the nature of the statement: - INSERT statements are GREEN (symbolic of creation or genesis) - SELECT statements are BLUE (typically used for informational displays, as SELECT statements do not normally have side-effects) - DELETE statements are RED (commonly used to indicate the danger of a destructive action) - UPDATE statements are YELLOW (it's like a less extreme RED :P) - TRANSACTION statements are CYAN (arbitrary) - and any other statements are MAGENTA (again, arbitrary)
1 parent 1022796 commit f5d8dd6

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

activerecord/lib/active_record/log_subscriber.rb

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,21 @@ def sql(event)
4747
binds = " " + payload[:binds].map { |attr| render_bind(attr) }.inspect
4848
end
4949

50-
if odd?
51-
name = color(name, CYAN, true)
52-
sql = color(sql, nil, true)
53-
else
54-
name = color(name, MAGENTA, true)
55-
end
50+
name = color(name, nil, true)
51+
sql = color(sql, sql_color(sql), true)
5652

5753
debug " #{name} #{sql}#{binds}"
5854
end
5955

60-
def odd?
61-
@odd = !@odd
56+
def sql_color(sql)
57+
case sql
58+
when /\s*\Ainsert/i then GREEN
59+
when /\s*\Aselect/i then BLUE
60+
when /\s*\Aupdate/i then YELLOW
61+
when /\s*\Adelete/i then RED
62+
when /transaction\s*\Z/i then CYAN
63+
else MAGENTA
64+
end
6265
end
6366

6467
def logger

0 commit comments

Comments
 (0)