Last active
April 14, 2017 16:07
-
-
Save seanlinsley/4670262 to your computer and use it in GitHub Desktop.
Revisions
-
seanlinsley revised this gist
May 2, 2013 . 1 changed file with 22 additions and 11 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -5,31 +5,42 @@ def formatter=(formatter) end end # Defines a custom log format (time, severity, message, PID, backtrace)... all with color! class Formatter SEVERITY_TO_TAG = {'DEBUG'=>'meh', 'INFO'=>'fyi', 'WARN'=>'hmm', 'ERROR'=>'wtf', 'FATAL'=>'omg', 'UNKNOWN'=>'???'} SEVERITY_TO_COLOR = {'DEBUG'=>'37', 'INFO'=>'32', 'WARN'=>'33', 'ERROR'=>'31', 'FATAL'=>'31', 'UNKNOWN'=>'37'} HUMOR_FOR_ENV = {development: true, test: true, production: false} DEPTH_FOR_ENV = {development: 3, test: 3, production: 1} EXCLUSION_REGEX = /log|active_support|active_record/ def humorous? return @is_humorous if defined? @is_humorous @is_humorous = HUMOR_FOR_ENV[ Rails.env.to_sym ] end def depth @depth ||= DEPTH_FOR_ENV[ Rails.env.to_sym ] end def call(severity, time, progname, msg) t = time.strftime("%Y-%m-%d %H:%M:%S.") << time.usec.to_s[0..2].rjust(3) color = SEVERITY_TO_COLOR[severity] sev = humorous? ? "%-3s" % SEVERITY_TO_TAG[severity] # pad to at least 3 characters : "%-5s" % severity # pad to at least 5 characters # 2013-05-01 19:16:00.785 [omg] oh noes! (pid:30976) (admin/user.rb:45:in `block (4 levels) in <top (required)>') <- `call' <- `content_for' <- `block (2 levels) in row' <- `block in build_tag' "\033[0;37m#{t}\033[0m [\033[#{color}m#{sev}\033[0m] #{msg.strip} (pid:#{$$}) #{whodunit}\033[0m\n" end def whodunit latest, *others = caller.select{ |a| a !~ EXCLUSION_REGEX }[0, depth] latest = latest[/(lib|app)\/(.*)/,-1] || latest string = "" string << "\033[36m(#{latest})" string << "\033[35m <- " + others.map{ |s| s[/`.*/] }.join(' <- ') if others.any? string end end Rails.logger.formatter = Formatter.new -
seanlinsley revised this gist
Apr 14, 2013 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -24,7 +24,7 @@ def call(severity, time, progname, msg) # Using the full backtrace, we attempt to identify the line of code that called us. # Show a shortened file path if possible. Else, show the full path. full_path = caller.detect{ |a| a !~ /log|active_support|active_record/ } whodunit = full_path[/(lib|app).*/] || full_path # 2013-01-29 19:48:16.976 [fyi] Served asset /active_admin.js - 304 Not Modified (3ms) (pid:6878) (lib/sprockets/server.rb:58:in `call') -
seanlinsley revised this gist
Mar 5, 2013 . 1 changed file with 3 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -22,10 +22,9 @@ def call(severity, time, progname, msg) sev = humorous? ? "%-3s" % SEVERITY_TO_TAG[severity] : "%-5s" % severity # Using the full backtrace, we attempt to identify the line of code that called us. # Show a shortened file path if possible. Else, show the full path. full_path = caller.detect{ |a| !(a =~ /log|active_support|active_record/) } whodunit = full_path[/(lib|app).*/] || full_path # 2013-01-29 19:48:16.976 [fyi] Served asset /active_admin.js - 304 Not Modified (3ms) (pid:6878) (lib/sprockets/server.rb:58:in `call') -
seanlinsley created this gist
Jan 30, 2013 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,36 @@ # Define a setter to pass in a custom log formatter class ActiveSupport::BufferedLogger def formatter=(formatter) @log.formatter = formatter end end # Define a custom log format (time, severity, message, PID, caller trace)... all with color! class Formatter SEVERITY_TO_TAG = {'DEBUG'=>'meh', 'INFO'=>'fyi', 'WARN'=>'hmm', 'ERROR'=>'wtf', 'FATAL'=>'omg', 'UNKNOWN'=>'???'} SEVERITY_TO_COLOR = {'DEBUG'=>'0;37', 'INFO'=>'32', 'WARN'=>'33', 'ERROR'=>'31', 'FATAL'=>'31', 'UNKNOWN'=>'37'} HUMOR_FOR_ENV = {development: true, test: true, production: false} def humorous? return @is_humorous if defined? @is_humorous @is_humorous = HUMOR_FOR_ENV[ Rails.env.to_sym ] end def call(severity, time, progname, msg) t = time.strftime("%Y-%m-%d %H:%M:%S.") << time.usec.to_s[0..2].rjust(3) color = SEVERITY_TO_COLOR[severity] sev = humorous? ? "%-3s" % SEVERITY_TO_TAG[severity] : "%-5s" % severity # Using the full backtrace, we identify the line of code that called us. # If possible, show a shortened file path (the regex). Else, show full path. # TODO: find a better solution to handle Rails logs which occur at different places in the backtrace full_path = caller.detect{ |a| !a.include? 'log' } whodunit = full_path[/(lib|app).*/] || full_path # 2013-01-29 19:48:16.976 [fyi] Served asset /active_admin.js - 304 Not Modified (3ms) (pid:6878) (lib/sprockets/server.rb:58:in `call') "\033[0;37m#{t}\033[0m [\033[#{color}m#{sev}\033[0m] #{msg.strip} (pid:#{$$}) \033[36m(#{whodunit})\033[0m\n" end end Rails.logger.formatter = Formatter.new