Class: Makitzo::Logging::Collector

Inherits:
Object
  • Object
show all
Includes:
Colorize
Defined in:
lib/makitzo/logging/collector.rb

Overview

A logger which collects all log messages and displays a summary by host

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Colorize

#bold, #colorize

Constructor Details

#initializeCollector

Returns a new instance of Collector.



11
12
13
14
15
16
17
18
# File 'lib/makitzo/logging/collector.rb', line 11

def initialize
  @use_color = true
  @host = nil
  @messages = []
  @hosts = Hash.new { |h,k| h[k] = {:error => false, :messages => []} }
  @lock = Mutex.new
  @silenced = false
end

Instance Attribute Details

#use_colorObject

Returns the value of attribute use_color.



8
9
10
# File 'lib/makitzo/logging/collector.rb', line 8

def use_color
  @use_color
end

Instance Method Details

#append(*chunks) ⇒ Object



141
142
143
144
145
146
147
# File 'lib/makitzo/logging/collector.rb', line 141

def append(*chunks)
  unless @silenced
    @lock.synchronize do
      active_log << chunks.join('').strip
    end
  end
end

#collector?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/makitzo/logging/collector.rb', line 117

def collector?
  true
end

#debug(message) ⇒ Object



113
114
115
# File 'lib/makitzo/logging/collector.rb', line 113

def debug(message)
  append blue('[DEBUG]', true), ' ', sanitize(message)
end

#error(message) ⇒ Object



92
93
94
95
# File 'lib/makitzo/logging/collector.rb', line 92

def error(message)
  append red("[ERROR]", true), ' ', red(sanitize(message))
  overall_error! if current_host
end

#info(message) ⇒ Object



109
110
111
# File 'lib/makitzo/logging/collector.rb', line 109

def info(message)
  append '[INFO]', ' ', sanitize(message)
end

#log_command(status, options = {}) ⇒ Object

logs a command options - override command line to be logged. useful for masking passwords.



47
48
49
50
51
52
53
54
# File 'lib/makitzo/logging/collector.rb', line 47

def log_command(status, options = {})
  command = options[:command] || status.command
  
  log_command_line(command, status.success?)
  log_command_status(status)
  
  overall_error! if current_host && !status.success?
end

#log_command_line(command, success = true) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/makitzo/logging/collector.rb', line 56

def log_command_line(command, success = true)
  if command.is_a?(Net::SSH::Connection::Session::ExecStatus)
    success = command.success?
    command = command.command
  end
  if success
    append green("$", true), " ", green(sanitize(command))
  else
    append red("$", true), " ", red(sanitize(command))
  end
end

#log_command_status(result, success = true) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/makitzo/logging/collector.rb', line 68

def log_command_status(result, success = true)
  if result.is_a?(Net::SSH::Connection::Session::ExecStatus)
    success = result.success?
    result  = (success ? result.stdout : result.stderr).last_line.strip
  end
  unless result.empty?
    if success
      append green("-", true), " ", green(sanitize(result))
    else
      append red("!", true), " ", red(sanitize(result))
    end
  end
end

#notice(message) ⇒ Object



101
102
103
# File 'lib/makitzo/logging/collector.rb', line 101

def notice(message)
  append cyan("[NOTICE]", true), ' ', sanitize(message)
end

#overall_error!Object



87
88
89
90
# File 'lib/makitzo/logging/collector.rb', line 87

def overall_error!
  raise "Cannot log host error when no host is set" unless current_host
  @hosts[current_host.to_s][:error] = true
end

#overall_success!Object



82
83
84
85
# File 'lib/makitzo/logging/collector.rb', line 82

def overall_success!
  raise "Cannot log host success when no host is set" unless current_host
  @hosts[current_host.to_s][:error] = false
end

#resultObject



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/makitzo/logging/collector.rb', line 121

def result
  out = ""
  
  @hosts.keys.sort.each do |host_name|
    host_status = @hosts[host_name]
    next if host_status[:messages].empty?
    out << magenta('* ' + host_name, true) << " " << (host_status[:error] ? red('[ERROR]', true) : green('[OK]', true)) << "\n"
    host_status[:messages].each { |m| out << m.indent(2) << "\n" }
    out << "\n"
  end
  
  unless @messages.empty?
    out << magenta("* Global Messages", true) << "\n"
    @messages.each { |m| out << m.indent(2) << "\n" }
    out << "\n"
  end
  
  out
end

#silence(&block) ⇒ Object

This method is not threadsafe. So call it before spawning threads.



21
22
23
24
25
26
27
28
29
# File 'lib/makitzo/logging/collector.rb', line 21

def silence(&block)
  begin
    was_silenced = @silenced
    @silenced = true
    yield if block_given?
  ensure
    @silenced = was_silenced
  end
end

#success(message) ⇒ Object



97
98
99
# File 'lib/makitzo/logging/collector.rb', line 97

def success(message)
  append green("[OK]", true), ' ', sanitize(message)
end

#use_color?Boolean

Returns:

  • (Boolean)


9
# File 'lib/makitzo/logging/collector.rb', line 9

def use_color?; !!@use_color; end

#warn(message) ⇒ Object



105
106
107
# File 'lib/makitzo/logging/collector.rb', line 105

def warn(message)
  append yellow("[WARNING]", true), ' ', sanitize(message)
end

#with_host(host, &block) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/makitzo/logging/collector.rb', line 31

def with_host(host, &block)
  return unless block_given?
  
  begin
    set_current_host(host)
    info("host is #{host.address}")
    yield
  ensure
    set_current_host(nil)
  end
  
  nil
end