class Sherlog::Parser
Public Class Methods
new(patterns = {}, filter = nil)
click to toggle source
# File lib/sherlog_holmes/parser.rb, line 28 def initialize(patterns = {}, filter = nil) @filter = filter @patterns = { exception: /^$/, stacktrace: /^$/ }.merge patterns @filter ||= filter { |entry| true } @listeners = [] end
Public Instance Methods
collect()
click to toggle source
# File lib/sherlog_holmes/parser.rb, line 38 def collect result = Result::new on_new_entry do |entry| result << entry end result end
filter(filter = nil, &block)
click to toggle source
# File lib/sherlog_holmes/parser.rb, line 51 def filter(filter = nil, &block) @filter = filter if filter @filter = Filter::new &block if block end
on_new_entry(listener = nil, &block)
click to toggle source
# File lib/sherlog_holmes/parser.rb, line 46 def on_new_entry(listener = nil, &block) listener ||= block @listeners << listener end
parse(input)
click to toggle source
# File lib/sherlog_holmes/parser.rb, line 60 def parse(input) entry = nil process = ParserProcess::new foreach input do |line| try_guess_pattern line unless @patterns[:entry] if @patterns[:entry] =~ line entry_data = Hash[Regexp.last_match.names.map { |k| [k.to_sym, Regexp.last_match[k].to_s.strip] }] # notify the last entry parsed notify entry if entry and @filter.accept? entry entry = Entry::new entry_data entry.process = process entry.raw_content = line.chomp entry.exceptions << Regexp.last_match[:exception] if @patterns[:exception] =~ entry.message else if entry if entry.exception? and @patterns[:stacktrace] =~ line entry.stacktrace << line.chomp else entry << line.chomp end entry.exceptions << Regexp.last_match[:exception] if @patterns[:exception] =~ line entry.raw_content << $/ << line.chomp end end break if process.stop_requested? end # notify the last entry parsed notify entry if entry and @filter.accept? entry end
patterns(config)
click to toggle source
# File lib/sherlog_holmes/parser.rb, line 56 def patterns(config) @patterns.merge! config end
Private Instance Methods
foreach(input, &block)
click to toggle source
# File lib/sherlog_holmes/parser.rb, line 92 def foreach(input, &block) if File.exist? input IO.foreach input, encoding: ENV['SHERLOG_FILE_ENCODE'], &block else input.each_line &block end end
notify(entry)
click to toggle source
# File lib/sherlog_holmes/parser.rb, line 100 def notify(entry) return if entry.process.stop_requested? @listeners.each do |listener| listener.call entry end end
try_guess_pattern(line)
click to toggle source
# File lib/sherlog_holmes/parser.rb, line 107 def try_guess_pattern(line) key, patterns = Sherlog.loaded_patterns.find do |key, patterns| patterns[:entry].match line if patterns[:entry] end @patterns.merge! patterns if patterns end