Class: HumorousLogFormatter::LogFormatter
- Inherits:
-
Object
- Object
- HumorousLogFormatter::LogFormatter
- Includes:
- ActiveSupport::TaggedLogging::Formatter
- Defined in:
- lib/humorous_log_formatter.rb
Constant Summary collapse
- SEVERITY_TO_TAG_MAP =
{'DEBUG' => 'meh', 'INFO' => 'fyi', 'WARN' => 'hmm', 'ERROR' => 'wtf', 'FATAL' => 'omg', 'UNKNOWN' => '???'}
- SEVERITY_TO_COLOR_MAP =
{'DEBUG' => '0;37', 'INFO' => '32', 'WARN' => '33', 'ERROR' => '31', 'FATAL' => '31', 'UNKNOWN' => '37'}
- TIME_FORMAT =
"%Y-%m-%d %H:%M:%S."- SKIP_TIME =
because heroku already prints the time, override if you use in prod and aren’t on Heroku
!Rails.env.development? # because heroku already prints the time, override if you use in prod and aren't on Heroku
- SUPER_TIME_PRECISION =
3- SUPER_TIME_PRECISION_STOP_INDEX =
SUPER_TIME_PRECISION - 1
- USE_SUPER_TIME =
SUPER_TIME_PRECISION > 0
- USE_HUMOROUS_SEVERITIES =
begin if ENV['LOG_HUMOR'] ENV['LOG_HUMOR'] != 'false' # Default to true else Rails.env.development? end end
- USE_COLOR =
begin if ENV['LOG_COLOR'] ENV['LOG_COLOR'] != 'false' # Default to true else Rails.env.development? end end
- THIS_FILE_PATH =
File.(".")
- FORMATTED_SEVERITY =
USE_HUMOROUS_SEVERITIES ? lambda { |severity| sprintf("%-3s", "#{SEVERITY_TO_TAG_MAP[severity]}") } : lambda { |severity| sprintf("%-5s", "#{severity}") }
- FORMATTED_MESSAGE =
if USE_COLOR lambda { |severity, formatted_time, msg| color = SEVERITY_TO_COLOR_MAP[severity] res = '' res << "[\033[#{color}m#{formatted_time}\033[0m] " if formatted_time res << "[\033[#{color}m#{FORMATTED_SEVERITY.call(severity)}\033[0m] #{msg.strip}\n" } else lambda { |severity, formatted_time, msg| res = '' res << "[#{formatted_time}]" if formatted_time res << "[#{FORMATTED_SEVERITY.call(severity)}] #{msg.strip}\n" } end
Instance Method Summary collapse
- #call(severity, time, progname, msg) ⇒ Object
- #colorize_exception(line) ⇒ Object
- #exception_values(e) ⇒ Object
Instance Method Details
#call(severity, time, progname, msg) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/humorous_log_formatter.rb', line 76 def call(severity, time, progname, msg) if SKIP_TIME formatted_time = nil else formatted_time = time.strftime(TIME_FORMAT) formatted_time << time.usec.to_s[0..(SUPER_TIME_PRECISION_STOP_INDEX)].rjust(SUPER_TIME_PRECISION) if USE_SUPER_TIME end text = if msg.is_a? String msg elsif msg.is_a? Exception " --> Exception:#{exception_values(msg)}" else "!!!!! UNKNOWN TYPE: #{msg.class} #{msg.to_s}" end FORMATTED_MESSAGE.call(severity, formatted_time, text) end |
#colorize_exception(line) ⇒ Object
72 73 74 |
# File 'lib/humorous_log_formatter.rb', line 72 def colorize_exception(line) "\033[01;32m#{line}\033[0m" end |
#exception_values(e) ⇒ Object
64 65 66 67 68 69 70 |
# File 'lib/humorous_log_formatter.rb', line 64 def exception_values(e) trace = e.backtrace.select { |x| !line.starts_with?(THIS_FILE_PATH) } trace = trace.map { |l| colorize_exception(l) } if USE_COLOR first = "\n#{trace.first}: #{e.} (#{e.class})" rest = "\t#{trace[1..-1].join("\n\t")}" "#{first}\n#{rest}" end |