Class: MemoryMonitoring::SimpleFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/memory_monitoring/simple_formatter.rb

Overview

日志格式

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' }
USE_HUMOROUS_SEVERITIES =
true

Instance Method Summary collapse

Instance Method Details

#call(severity, time, progname, msg) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/memory_monitoring/simple_formatter.rb', line 9

def call(severity, time, progname, msg)
  if USE_HUMOROUS_SEVERITIES
    formatted_severity = sprintf("%-3s", SEVERITY_TO_TAG_MAP[severity])
  else
    formatted_severity = sprintf("%-5s", severity)
  end
   
  formatted_time = time.strftime("%Y-%m-%d %H:%M:%S.") << time.usec.to_s[0..2].rjust(3)
  color = SEVERITY_TO_COLOR_MAP[severity]
   
  "\033[0;37m#{formatted_time}\033[0m [\033[#{color}m#{formatted_severity}\033[0m] #{msg.strip} (pid:#{$$})\n"
end