Class: Capistrano::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/capistrano/logger.rb

Constant Summary collapse

CAP_COLORS =
{ 
   :none     => "0",
   :black    => "30",
   :red      => "31",
   :green    => "32",
   :yellow   => "33",
   :blue     => "34",
   :magenta  => "35",
   :cyan     => "36",
   :white    => "37"
}
CAP_ATTRIBUTES =
{
  :bright     => 1,
  :dim        => 2,
  :underscore => 4,
  :blink      => 5,
  :reverse    => 7,
  :hidden     => 8
}
@@color_matchers =
[]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add_color_matcher(options) ⇒ Object

:nodoc:



59
60
61
# File 'lib/capistrano/logger.rb', line 59

def self.add_color_matcher( options ) #:nodoc:
  @@color_matchers.push( options )
end

Instance Method Details

#colorize(message, color, attribute, nl = "\n") ⇒ Object



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

def colorize(message, color, attribute, nl = "\n")
  attribute = "#{attribute};" if attribute
  "\e[#{attribute}#{color}m" + message.strip + "\e[0m#{nl}"
end

#log(level, message, line_prefix = nil) ⇒ Object Also known as: org_log

:nodoc:



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/capistrano/logger.rb', line 28

def log(level, message, line_prefix=nil) #:nodoc:
  
  color = :none
  attribute = nil
  
  # Sort matchers in reverse order so we can break if we found a match.
  @@sorted_color_matchers ||= @@color_matchers.sort_by { |i| -i[:prio] }
  
  @@sorted_color_matchers.each do |filter|
    
    if (filter[:level] == level || filter[:level].nil?)
      if message =~ filter[:match]
        color = filter[:color]
        attribute = filter[:attribute]
        message = filter[:prepend] + message unless filter[:prepend].nil?
        break
      end
    end
    
  end

  if color != :hide
    current_color = CAP_COLORS[color]
    current_attribute = CAP_ATTRIBUTES[attribute]

    line_prefix = colorize(line_prefix.to_s, current_color, current_attribute, nil) unless line_prefix.nil?
    org_log(level, colorize(message, current_color, current_attribute), line_prefix)
  end
        
end