Class: Arrow::Logger::ColorOutputter

Inherits:
FileOutputter show all
Defined in:
lib/arrow/logger/coloroutputter.rb

Overview

A derivative of Arrow::Logger::FileOutputter that outputs logging messages in ANSI colors according to their level.

Authors

Please see the file LICENSE in the top-level directory for licensing details.

Constant Summary collapse

AnsiAttributes =

Set some ANSI escape code constants (Shamelessly stolen from Perl’s Term::ANSIColor by Russ Allbery <[email protected]> and Zenin <[email protected]>

{
  'clear'      => 0,
  'reset'      => 0,
  'bold'       => 1,
  'dark'       => 2,
  'underline'  => 4,
  'underscore' => 4,
  'blink'      => 5,
  'reverse'    => 7,
  'concealed'  => 8,

  'black'      => 30,   'on_black'   => 40, 
  'red'        => 31,   'on_red'     => 41, 
  'green'      => 32,   'on_green'   => 42, 
  'yellow'     => 33,   'on_yellow'  => 43, 
  'blue'       => 34,   'on_blue'    => 44, 
  'magenta'    => 35,   'on_magenta' => 45, 
  'cyan'       => 36,   'on_cyan'    => 46, 
  'white'      => 37,   'on_white'   => 47
}
DEFAULT_COLOR_SCHEME =

Default color map: :level => %wscheme

{
  :debug   => %w{dark white},
  :info    => %w{cyan},
  :notice    => %w{bold cyan},
  :warning => %w{bold yellow},
  :error   => %w{bold red},
  :crit    => %w{bold white on_red},
  :alert   => %w{bold blink white on_red},
  :emerg   => %w{bold blink yellow on_red},
}
DEFAULT_DESCRIPTION =

Default decription used when creating instances

"Logging Outputter"
DEFAULT_FORMAT =

The default logging output format

%q{#{time.strftime('%Y/%m/%d %H:%M:%S')} [#{level}]: #{name} } +
%q{#{frame ? '('+frame+')' : ''}: #{msg}}

Instance Attribute Summary collapse

Attributes inherited from FileOutputter

#io

Attributes inherited from Outputter

#description, #format

Instance Method Summary collapse

Methods inherited from Outputter

create, derivativeDirs, #inspect, parse_uri

Constructor Details

#initialize(uri, description = DEFAULT_DESCRIPTION, format = DEFAULT_FORMAT) ⇒ ColorOutputter

Override the default to add color scheme instance variable



63
64
65
66
# File 'lib/arrow/logger/coloroutputter.rb', line 63

def initialize( uri, description=DEFAULT_DESCRIPTION, format=DEFAULT_FORMAT ) # :notnew:
  super
  @color_scheme = DEFAULT_COLOR_SCHEME.dup
end

Instance Attribute Details

#color_schemeObject (readonly)

The color scheme hash for this logger



74
75
76
# File 'lib/arrow/logger/coloroutputter.rb', line 74

def color_scheme
  @color_scheme
end

Instance Method Details

#write(time, level, name, frame, msg) ⇒ Object

Write the given level, name, frame, and msg to the logfile.



78
79
80
81
82
83
84
# File 'lib/arrow/logger/coloroutputter.rb', line 78

def write( time, level, name, frame, msg )
  colors = @color_scheme[level]
  super do |msg|
    color_msg = colorize( msg, colors )
    @io.puts( color_msg )
  end
end