Class: LogWeaver::ParsedLog

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(prefix, log) ⇒ ParsedLog

Returns a new instance of ParsedLog.



8
9
10
11
# File 'lib/log_weaver/parsed_log.rb', line 8

def initialize(prefix, log)
  @prefix = prefix
  @lines = ParsedLog.parse_log log
end

Instance Attribute Details

#linesObject

TODO: rename; attr_reader should suffice



5
6
7
# File 'lib/log_weaver/parsed_log.rb', line 5

def lines
  @lines
end

#prefixObject (readonly)

Returns the value of attribute prefix.



6
7
8
# File 'lib/log_weaver/parsed_log.rb', line 6

def prefix
  @prefix
end

Class Method Details

.extract_time_stamp(line) ⇒ Object



33
34
35
36
37
38
# File 'lib/log_weaver/parsed_log.rb', line 33

def self.extract_time_stamp(line)
  timestamp = line[/^[0-9]{4}-[01][0-9]-[0-3][0-9] [0-2][0-9](:[0-5][0-9]){2}\.[0-9]{3}/,0]
  message = line.sub(/^#{timestamp}/,'')
  timestamp = Time.parse(timestamp) unless timestamp.nil?
  [timestamp,message]
end

.parse_log(log) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/log_weaver/parsed_log.rb', line 15

def self.parse_log(log)
  res = {}
  previous_key = nil
  log.string.split("\n").each do |line|
    (timestamp, message) = extract_time_stamp(line)
    if timestamp
      key = timestamp
      res[key] = [] unless key == previous_key
      previous_key = key
    else
      raise ArgumentError, "Log does not begin with a timestamp." if previous_key.nil?
    end

    res[previous_key] << line #message
  end
  res
end