Class: Capistrano::Logger

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

Overview

:nodoc:

Constant Summary collapse

IMPORTANT =
0
INFO =
1
DEBUG =
2
TRACE =
3
MAX_LEVEL =
3
COLORS =
{
  :none     => "0",
  :black    => "30",
  :red      => "31",
  :green    => "32",
  :yellow   => "33",
  :blue     => "34",
  :magenta  => "35",
  :cyan     => "36",
  :white    => "37"
}
STYLES =
{
  :bright     => 1,
  :dim        => 2,
  :underscore => 4,
  :blink      => 5,
  :reverse    => 7,
  :hidden     => 8
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Logger

Returns a new instance of Logger.



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/capistrano/logger.rb', line 78

def initialize(options={})
  output = options[:output] || $stderr
  if output.respond_to?(:puts)
    @device = output
  else
    @device = File.open(output.to_str, "a")
    @needs_close = true
  end

  @options = options
  @level = options[:level] || 0
  @disable_formatters = options[:disable_formatters]
end

Instance Attribute Details

#deviceObject

Returns the value of attribute device.



3
4
5
# File 'lib/capistrano/logger.rb', line 3

def device
  @device
end

#disable_formattersObject

Returns the value of attribute disable_formatters.



3
4
5
# File 'lib/capistrano/logger.rb', line 3

def disable_formatters
  @disable_formatters
end

#levelObject

Returns the value of attribute level.



3
4
5
# File 'lib/capistrano/logger.rb', line 3

def level
  @level
end

Class Method Details

.add_formatter(options) ⇒ Object

:nodoc:



67
68
69
70
# File 'lib/capistrano/logger.rb', line 67

def add_formatter(options) #:nodoc:
  @formatters.push(options)
  @sorted_formatters = nil
end

.default_formattersObject



55
56
57
# File 'lib/capistrano/logger.rb', line 55

def default_formatters
  @default_formatters
end

.default_formatters=(defaults = nil) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/capistrano/logger.rb', line 59

def default_formatters=(defaults=nil)
  @default_formatters = [defaults].flatten

  # reset the formatters
  @formatters = @default_formatters
  @sorted_formatters = nil
end

.sorted_formattersObject



72
73
74
75
# File 'lib/capistrano/logger.rb', line 72

def sorted_formatters
  # Sort matchers in reverse order so we can break if we found a match.
  @sorted_formatters ||= @formatters.sort_by { |i| -(i[:priority] || i[:prio] || 0) }
end

Instance Method Details

#closeObject



92
93
94
# File 'lib/capistrano/logger.rb', line 92

def close
  device.close if @needs_close
end

#debug(message, line_prefix = nil) ⇒ Object



153
154
155
# File 'lib/capistrano/logger.rb', line 153

def debug(message, line_prefix=nil)
  log(DEBUG, message, line_prefix)
end

#format(message, color, style, nl = "\n") ⇒ Object



161
162
163
164
# File 'lib/capistrano/logger.rb', line 161

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

#important(message, line_prefix = nil) ⇒ Object



145
146
147
# File 'lib/capistrano/logger.rb', line 145

def important(message, line_prefix=nil)
  log(IMPORTANT, message, line_prefix)
end

#info(message, line_prefix = nil) ⇒ Object



149
150
151
# File 'lib/capistrano/logger.rb', line 149

def info(message, line_prefix=nil)
  log(INFO, message, line_prefix)
end

#log(level, message, line_prefix = nil) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/capistrano/logger.rb', line 96

def log(level, message, line_prefix=nil)
  if level <= self.level
    # Only format output if device is a TTY or formatters are not disabled
    if device.tty? && !@disable_formatters
      color = :none
      style = nil

      Logger.sorted_formatters.each do |formatter|
        if (formatter[:level] == level || formatter[:level].nil?)
          if message =~ formatter[:match] || line_prefix =~ formatter[:match]
            color = formatter[:color] if formatter[:color]
            style = formatter[:style] || formatter[:attribute] # (support original cap colors)
            message.gsub!(formatter[:match], formatter[:replace]) if formatter[:replace]
            message = formatter[:prepend] + message unless formatter[:prepend].nil?
            message = message + formatter[:append] unless formatter[:append].nil?
            message = Time.now.strftime('%Y-%m-%d %T') + ' ' + message if formatter[:timestamp]
            break unless formatter[:replace]
          end
        end
      end

      if color == :hide
        # Don't do anything if color is set to :hide
        return false
      end

      term_color = COLORS[color]
      term_style = STYLES[style]

      # Don't format message if no color or style
      unless color == :none and style.nil?
        unless line_prefix.nil?
          line_prefix = format(line_prefix, term_color, term_style, nil)
        end
        message = format(message, term_color, term_style)
      end
    end

    indent = "%*s" % [MAX_LEVEL, "*" * (MAX_LEVEL - level)]
    (RUBY_VERSION >= "1.9" ? message.lines : message).each do |line|
      if line_prefix
        device.puts "#{indent} [#{line_prefix}] #{line.strip}\n"
      else
        device.puts "#{indent} #{line.strip}\n"
      end
    end
  end
end

#trace(message, line_prefix = nil) ⇒ Object



157
158
159
# File 'lib/capistrano/logger.rb', line 157

def trace(message, line_prefix=nil)
  log(TRACE, message, line_prefix)
end