Class: Lumberjack::LogEntryMatcher

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

Overview

A flexible matching utility for testing and filtering log entries based on multiple criteria. This class provides pattern-based matching against log entry components including message content, severity levels, program names, and custom attributes with support for nested attribute structures.

The matcher uses Ruby’s case equality operator (===) for flexible matching, supporting exact values, regular expressions, ranges, classes, and other pattern matching constructs. It’s primarily designed for use with the Test device in testing scenarios but can be used anywhere log entry filtering is needed.

See Also:

Defined Under Namespace

Classes: Score

Instance Method Summary collapse

Constructor Details

#initialize(message: nil, severity: nil, progname: nil, attributes: nil) ⇒ LogEntryMatcher

Create a new log entry matcher with optional filtering criteria. All parameters are optional and nil values indicate no filtering for that component. The matcher uses case equality (===) for flexible pattern matching against each specified criterion.

Parameters:

  • message (Object, nil) (defaults to: nil)

    Pattern to match against log entry messages. Supports strings, regular expressions, or any object responding to ===

  • severity (Integer, String, Symbol, nil) (defaults to: nil)

    Severity level to match. Accepts numeric levels or symbolic names (:debug, :info, etc.)

  • progname (Object, nil) (defaults to: nil)

    Pattern to match against program names. Supports strings, regular expressions, or any object responding to ===

  • attributes (Hash, nil) (defaults to: nil)

    Hash of attribute patterns to match against log entry attributes. Supports nested attribute matching and dot notation



32
33
34
35
36
37
38
# File 'lib/lumberjack/log_entry_matcher.rb', line 32

def initialize(message: nil, severity: nil, progname: nil, attributes: nil)
  message = message.strip if message.is_a?(String)
  @message_filter = message
  @severity_filter = Severity.coerce(severity) if severity
  @progname_filter = progname
  @attributes_filter = Utils.expand_attributes(attributes) if attributes
end

Instance Method Details

#closest(entries) ⇒ Lumberjack::LogEntry?

Find the closest matching log entry from a list of candidates. This method scores each entry based on how well it matches the specified criteria and returns the entry with the highest score, provided it meets a minimum threshold. If no entries meet the threshold, nil is returned.

Parameters:

Returns:



66
67
68
69
70
# File 'lib/lumberjack/log_entry_matcher.rb', line 66

def closest(entries)
  scored_entries = entries.map { |entry| [entry, entry_score(entry)] }
  best_score = scored_entries.max_by { |_, score| score }
  (best_score&.last.to_f >= Score::MIN_SCORE_THRESHOLD) ? best_score.first : nil
end

#match?(entry) ⇒ Boolean

Test whether a log entry matches all specified criteria. The entry must satisfy all non-nil filter conditions to be considered a match. Uses case equality (===) for flexible pattern matching.

Parameters:

Returns:

  • (Boolean)

    True if the entry matches all specified criteria, false otherwise



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/lumberjack/log_entry_matcher.rb', line 46

def match?(entry)
  return false unless match_filter?(entry.message, @message_filter)
  return false unless match_filter?(entry.severity, @severity_filter)
  return false unless match_filter?(entry.progname, @progname_filter)

  if @attributes_filter
    attributes = Utils.expand_attributes(entry.attributes)
    return false unless match_attributes?(attributes, @attributes_filter)
  end

  true
end