Class: Loggability::Formatter::Color
- Inherits:
-
Default
- Object
- Loggability::Formatter
- Default
- Loggability::Formatter::Color
- 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
Attributes inherited from Loggability::Formatter
Instance Method Summary collapse
-
#initialize ⇒ Color
constructor
Create a new formatter.
-
#msg2str(msg, severity) ⇒ Object
Format the specified
msg
for output to the log.
Methods inherited from Default
Methods inherited from Loggability::Formatter
Constructor Details
#initialize ⇒ Color
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 |