Class: LogParser

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

Overview

Parse lines of log output and extract relevant information from them.

Instance Method Summary collapse

Constructor Details

#initializeLogParser

Returns a new instance of LogParser.



80
81
82
# File 'lib/kamalx.rb', line 80

def initialize
  @hostnames = {}
end

Instance Method Details

#parse(line) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/kamalx.rb', line 84

def parse(line)
  case line
  when /^(\w+.+)\.{3}$/
    [:white, [:bold, 'Stage:'], " #{::Regexp.last_match(1)}"]
  when /INFO \[(\w+)\] Running (.+) on (.+)/
    command_id = ::Regexp.last_match(1)
    hostname = ::Regexp.last_match(3)
    @hostnames[command_id] = hostname
    [:green, [:bold, "Command[#{command_id}@#{hostname}]"], " #{::Regexp.last_match(2)}"]
  when /INFO \[(\w+)\] Finished in ([\d.]+) seconds with exit status (\d+)/
    command_id = ::Regexp.last_match(1)
    hostname = @hostnames[command_id] || @hostnames.values.first || 'localhost'
    status_color = ::Regexp.last_match(3).to_i == 0 ? :green : :red
    [:yellow, [:bold, "Command[#{command_id}@#{hostname}]"], ' Returned Status: ', status_color,
     ::Regexp.last_match(3)]
  when /DEBUG \[(\w+)\] (.+)/
    [:yellow, [:bold, "Command[#{::Regexp.last_match(1)}@localhost]"], " #{::Regexp.last_match(2)}"]
  when /INFO (.+)/
    [:blue, [:bold, 'Info:'], " #{::Regexp.last_match(1)}"]
  else
    [:white, line]
  end
end