Class: Depends::Log::Formatter

Inherits:
Logger::Formatter
  • Object
show all
Defined in:
lib/depends/log/formatter.rb

Constant Summary collapse

COLOR_MAP =
{'DEBUG' =>'0;37', 'INFO'=>'32', 'WARN'=>'33', 'ERROR'=>'31', 'FATAL'=>'31', 'UNKNOWN'=>'37'}
@@show_time =
false

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.show_time=(show = false) ⇒ Object



11
12
13
# File 'lib/depends/log/formatter.rb', line 11

def self.show_time=(show=false)
  @@show_time = show
end

Instance Method Details

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



15
16
17
18
19
20
21
22
23
# File 'lib/depends/log/formatter.rb', line 15

def call(severity, time, progname, msg)
  color = COLOR_MAP[severity]

  if @@show_time
    sprintf("\033[0;37m%s \033[0m[\033[#{color}m%s\033[0m]: \033[0m%s\n", time.rfc2822(), severity, msg2str(msg))
  else
    sprintf("\033[#{color}m%s\033[0;37m: \033[0m%s\n", severity, msg2str(msg))
  end
end

#msg2str(msg) ⇒ Object

Converts some argument to a Logger.severity() call to a string. Regular strings pass through like normal, Exceptions get formatted as “message (class)nbacktrace”, and other random stuff gets put through “object.inspect”



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/depends/log/formatter.rb', line 28

def msg2str(msg)
  case msg
  when ::String
    msg
  when ::Exception
    "#{ msg.message } (#{ msg.class })\n" <<
      (msg.backtrace || []).join("\n")
  else
    msg.inspect
  end
end