Class: Loggability::Formatter::Color

Inherits:
Default show all
Defined in:
lib/loggability/formatter/color.rb

Overview

ANSI color log formatter. Outputs log messages color-coded by their severity if the current terminal appears to support it.

Constant Summary collapse

RESET =

ANSI reset

"\e[0m"
LEVEL_CODES =

ANSI color escapes keyed by severity

{
  :debug => "\e[1;30m", # bold black
  :info  =>  "\e[37m",   # white
  :warn  =>  "\e[1;33m", # bold yellow
  :error => "\e[31m",   # red
  :fatal => "\e[1;31m", # bold red
}
COLOR_TERMINAL_NAMES =

Pattern for matching color terminals

/(?:vt10[03]|xterm(?:-color)?|linux|screen)/i

Constants inherited from Default

Default::DEFAULT_DATETIME_FORMAT, Default::FORMAT

Instance Attribute Summary

Attributes inherited from Default

#datetime_format, #format

Attributes inherited from Loggability::Formatter

#derivatives

Instance Method Summary collapse

Methods inherited from Default

#call

Methods inherited from Loggability::Formatter

#call, create, inherited

Constructor Details

#initializeColor

Create a new formatter.



31
32
33
34
# File 'lib/loggability/formatter/color.rb', line 31

def initialize( * )
  super
  @color_enabled = COLOR_TERMINAL_NAMES.match( ENV['TERM'] ) ? true : false
end

Instance Method Details

#msg2str(msg, severity) ⇒ Object

Format the specified msg for output to the log.



42
43
44
45
46
47
48
49
50
# File 'lib/loggability/formatter/color.rb', line 42

def msg2str( msg, severity )
  msg = super
  if @color_enabled
    color = severity.downcase.to_sym
    msg = [ LEVEL_CODES[color], msg, RESET ].join( '' )
  end

  return msg
end