|
2 | 2 | require "logstash/filters/base" |
3 | 3 | require "logstash/namespace" |
4 | 4 | require "logstash/environment" |
| 5 | + require "logstash/event" |
5 | 6 | require "logstash/patterns/core" |
6 | 7 | require "grok-pure" # rubygem 'jls-grok' |
7 | 8 | require "set" |
@@ -237,6 +238,11 @@ class LogStash::Filters::Grok < LogStash::Filters::Base |
237 | 238 | # will be parsed and `hello world` will overwrite the original message. |
238 | 239 | config :overwrite, :validate => :array, :default => [] |
239 | 240 |
|
| 241 | + # If this attribute is set, the output of this filter will be an array |
| 242 | + # of objects written to the key supplied in this config value, this is |
| 243 | + # useful if your input is an array of messages to match. |
| 244 | + config :output_objects, :validate => :string, :default => nil |
| 245 | + |
240 | 246 | attr_reader :timeout_enforcer |
241 | 247 |
|
242 | 248 | # Register default pattern paths |
@@ -331,21 +337,30 @@ def match(groks, field, event) |
331 | 337 | @logger.warn("Grok regexp threw exception", :exception => e.message, :backtrace => e.backtrace, :class => e.class.name) |
332 | 338 | return false |
333 | 339 | end |
334 | | - |
| 340 | + |
335 | 341 | private |
336 | 342 | def match_against_groks(groks, field, input, event) |
| 343 | + target_event = @output_objects ? LogStash::Event.new : event |
| 344 | + |
337 | 345 | input = input.to_s |
338 | 346 | matched = false |
339 | 347 | groks.each do |grok| |
340 | 348 | # Convert anything else to string (number, hash, etc) |
341 | 349 |
|
342 | 350 | matched = @timeout_enforcer.grok_till_timeout(grok, field, input) |
343 | 351 | if matched |
344 | | - grok.capture(matched) {|field, value| handle(field, value, event)} |
| 352 | + grok.capture(matched) {|field, value| handle(field, value, target_event)} |
345 | 353 | break if @break_on_match |
346 | 354 | end |
347 | 355 | end |
348 | | - |
| 356 | + |
| 357 | + if @output_objects |
| 358 | + output_array = event.get(@output_objects) |
| 359 | + output_array = [] unless output_array.is_a? Array |
| 360 | + output_array << target_event.to_hash |
| 361 | + event.set(@output_objects, output_array) |
| 362 | + end |
| 363 | + |
349 | 364 | matched |
350 | 365 | end |
351 | 366 |
|
|
0 commit comments