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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Logger

Returns a new instance of Logger.



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/capistrano/logger.rb', line 13

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 = 0
end

Instance Attribute Details

#deviceObject (readonly)

Returns the value of attribute device.



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

def device
  @device
end

#levelObject

Returns the value of attribute level.



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

def level
  @level
end

Instance Method Details

#closeObject



26
27
28
# File 'lib/capistrano/logger.rb', line 26

def close
  device.close if @needs_close
end

#debug(message, line_prefix = nil) ⇒ Object



51
52
53
# File 'lib/capistrano/logger.rb', line 51

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

#important(message, line_prefix = nil) ⇒ Object



43
44
45
# File 'lib/capistrano/logger.rb', line 43

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

#info(message, line_prefix = nil) ⇒ Object



47
48
49
# File 'lib/capistrano/logger.rb', line 47

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

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



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/capistrano/logger.rb', line 30

def log(level, message, line_prefix=nil)
  if level <= self.level
    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



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

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