lib/grok-pure.rb in jls-grok-0.9.2 vs lib/grok-pure.rb in jls-grok-0.9.4
- old
+ new
@@ -46,29 +46,33 @@
public
def add_pattern(name, pattern)
@logger.info("Adding pattern", name => pattern)
@patterns[name] = pattern
return nil
- end
+ end # def add_pattern
public
def add_patterns_from_file(path)
file = File.new(path, "r")
file.each do |line|
- next if line =~ /^\s*#/
+ # Skip comments
+ next if line =~ /^\s*#/
+ # File format is: NAME ' '+ PATTERN '\n'
name, pattern = line.gsub(/^\s*/, "").split(/\s+/, 2)
+ # If the line is malformed, skip it.
next if pattern.nil?
+ # Trim newline and add the pattern.
add_pattern(name, pattern.chomp)
end
return nil
end # def add_patterns_from_file
public
def compile(pattern)
@capture_map = {}
- iterations_left = 100
+ iterations_left = 1000
@pattern = pattern
@expanded_pattern = pattern
index = 0
# Replace any instances of '%{FOO}' with that pattern.
@@ -86,21 +90,37 @@
if @patterns.include?(m["pattern"])
# create a named capture index that we can push later as the named
# pattern. We do this because ruby regexp can't capture something
# by the same name twice.
- p = @patterns[m["pattern"]]
+ regex = @patterns[m["pattern"]]
+ #puts "patterns[#{m["pattern"]}] => #{regex}"
capture = "a#{index}" # named captures have to start with letters?
#capture = "%04d" % "#{index}" # named captures have to start with letters?
- replacement_pattern = "(?<#{capture}>#{p})"
- #p(:input => m[0], :pattern => replacement_pattern)
+ replacement_pattern = "(?<#{capture}>#{regex})"
@capture_map[capture] = m["name"]
- @expanded_pattern.sub!(m[0], replacement_pattern)
+
+ #puts "Before: #{@expanded_pattern}"
+ #puts "m[0]: #{m[0]}"
+ #puts "replacement_pattern => #{replacement_pattern}"
+ #puts "Proposed: #{@expanded_pattern.sub(m[0], replacement_pattern)}"
+
+ # Ruby's String#sub() has a bug (or misfeature) that causes it to do bad
+ # things to backslashes in string replacements, so let's work around it
+ # See this gist for more details: https://gist.github.com/1491437
+ # This hack should resolve LOGSTASH-226.
+ @expanded_pattern.sub!(m[0]) { |s| replacement_pattern }
+
+ #puts "After: #{@expanded_pattern}"
+ #puts "m[0]: #{m[0]}"
+ #puts "replacement_pattern => #{replacement_pattern}"
index += 1
end
end
+ #@logger.debug("Finished expanding", :string => @expanded_pattern)
+ #puts "Expanded: #{@expanded_pattern}"
@regexp = Regexp.new(@expanded_pattern)
@logger.debug("Grok compiled OK", :pattern => pattern,
:expanded_pattern => @expanded_pattern)
end # def compile