Class: Lumberjack::LogEntryMatcher
- Inherits:
-
Object
- Object
- Lumberjack::LogEntryMatcher
- 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.
Defined Under Namespace
Classes: Score
Instance Method Summary collapse
-
#closest(entries) ⇒ Lumberjack::LogEntry?
Find the closest matching log entry from a list of candidates.
-
#initialize(message: nil, severity: nil, progname: nil, attributes: nil) ⇒ LogEntryMatcher
constructor
Create a new log entry matcher with optional filtering criteria.
-
#match?(entry) ⇒ Boolean
Test whether a log entry matches all specified criteria.
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.
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) = .strip if .is_a?(String) = @severity_filter = Severity.coerce(severity) if severity @progname_filter = progname @attributes_filter = Utils.(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.
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.
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., ) return false unless match_filter?(entry.severity, @severity_filter) return false unless match_filter?(entry.progname, @progname_filter) if @attributes_filter attributes = Utils.(entry.attributes) return false unless match_attributes?(attributes, @attributes_filter) end true end |