Module: LogParser::RegExpPattern

Overview

Represents a certain pattern log message. The model we use is that

* a pattern tells you if a message starts in a given line, and
* reads lines from there on until it ends.

The result will be a Message.

This type of pattern is characterized mainly by two regular expressions:

* One for matching the first line of a message, and
* one for matching the last line (+- 1) of a message, which may depend on the first.

Instance Method Summary collapse

Instance Method Details

#begins_at?(line) ⇒ true, false

Checks if this message pattern matches the given line, that is whether the ‘start` regexp (see #initialize) matches it.

Parameters:

  • line (String)

    The log line currently under investigation.

Returns:

  • (true, false)

    ‘true` if (and only if) this pattern can parse a single message from the given line onwards.



83
84
85
86
87
# File 'lib/log_parser/pattern.rb', line 83

def begins_at?(line)
  match = @start.match(line)
  @start_match = match unless match.nil?
  !match.nil?
end

#initialize(start, ending = { pattern: ->(_) { /^\s*$/ }, until: :match, inclusive: false }) ⇒ Object

Creates a new instance.

Parameters:

  • start (Regexp)

    Used to determine where a message starts. See also #begins_at?.

  • ending (Hash) (defaults to: { pattern: ->(_) { /^\s*$/ }, until: :match, inclusive: false })

    Used to determine where a message ends. See also #ends_at?.

Options Hash (ending):

  • :pattern (Regexp)

    Describes either what all lines of a message look like, or matches the first line not part of the same message.

  • :until (:match, :mismatch)

    Determines what is matched against ‘:pattern`.

    * If `:match`, messages end at the first line that _matches_ `:pattern`.
    * If `:mismatch`, messages end at the first line that does _not_ match `:pattern`.
    
  • :inclusive (true, false)

    Determines whether the first line that (mis)matched ‘:pattern` should be part of the message.



69
70
71
72
73
74
# File 'lib/log_parser/pattern.rb', line 69

def initialize(start, ending = { pattern: ->(_) { /^\s*$/ },
                                 until: :match,
                                 inclusive: false })
  @start = start
  @ending = ending
end

#read(lines) ⇒ Array<(Message, Int)>

Reads a message from the given lines.

Parameters:

Returns:

  • (Array<(Message, Int)>)

    An array of the message that was read, and the number of lines that it spans.

Raises:

  • If no message end could be found among the given lines.



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/log_parser/pattern.rb', line 97

def read(lines)
  ending = lines.find_index(1) { |l| ends_at?(l) }
  raise "Did not find end of message (pattern '#{self.class}')." if ending.nil?
  ending -= 1 unless @ending[:inclusive]

  # Use ending+1 since ending is the index when we drop the first line!
  msg = LogParser::Message.new(message: lines[0, ending + 1].join("\n"),
                               preformatted: true, level: nil,
                               pattern: self.class)
  [msg, ending + 1]
end