lib/grok-pure.rb in jls-grok-0.9.0 vs lib/grok-pure.rb in jls-grok-0.9.1

- old
+ new

@@ -1,14 +1,17 @@ require "rubygems" +require "logger" +require "cabin" # TODO(sissel): Check if 'grok' c-ext has been loaded and abort? class Grok attr_accessor :pattern attr_accessor :expanded_pattern + attr_accessor :logger PATTERN_RE = \ - /%{ # match '%{' not prefixed with '\' + /%\{ # match '%{' not prefixed with '\' (?<name> # match the pattern name (?<pattern>[A-z0-9]+) (?::(?<subname>[A-z0-9_:]+))? ) (?:=(?<definition> @@ -17,11 +20,11 @@ | (?<curly>\{(?:(?>[^{}]+|(?>\\[{}])+)|(\g<curly>))*\})+ )+ ))? [^}]* - }/x + \}/x GROK_OK = 0 GROK_ERROR_FILE_NOT_ACCESSIBLE = 1 GROK_ERROR_PATTERN_NOT_FOUND = 2 GROK_ERROR_UNEXPECTED_READ_SIZE = 3 @@ -31,27 +34,28 @@ GROK_ERROR_NOMATCH = 7 public def initialize @patterns = {} + @logger = Cabin::Channel.new + @logger.subscribe(Logger.new(STDOUT)) # TODO(sissel): Throw exception if we aren't using Ruby 1.9.2 or newer. end # def initialize public def add_pattern(name, pattern) - #puts "#{name} => #{pattern}" + @logger.info("Adding pattern", name => pattern) @patterns[name] = pattern return nil end public def add_patterns_from_file(path) file = File.new(path, "r") file.each do |line| next if line =~ /^\s*#/ - #puts "Pattern: #{line}" name, pattern = line.gsub(/^\s*/, "").split(/\s+/, 2) next if pattern.nil? add_pattern(name, pattern.chomp) end return nil @@ -94,10 +98,12 @@ index += 1 end end @regexp = Regexp.new(@expanded_pattern) + @logger.debug("Grok compiled OK", :pattern => pattern, + :expanded_pattern => @expanded_pattern) end # def compile public def match(text) match = @regexp.match(text) @@ -106,9 +112,10 @@ grokmatch = Grok::Match.new grokmatch.subject = text grokmatch.start, grokmatch.end = match.offset(0) grokmatch.grok = self grokmatch.match = match + @logger.debug("Regexp match object", :names => match.names, :captures => match.captures) return grokmatch else return false end end # def match