Class: LogStash::Filters::CSV

Inherits:
Base
  • Object
show all
Defined in:
lib/logstash/filters/csv.rb

Overview

The CSV filter takes an event field containing CSV data, parses it, and stores it as individual fields (can optionally specify the names). This filter can also parse data with any separator, not just commas.

Instance Method Summary collapse

Instance Method Details

#filter(event) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/logstash/filters/csv.rb', line 47

def filter(event)
  

  @logger.debug("Running csv filter", :event => event)

  matches = 0

  if event[@source]
    if event[@source].is_a?(String)
      event[@source] = [event[@source]]
    end

    if event[@source].length > 1
      @logger.warn("csv filter only works on fields of length 1",
                   :source => @source, :value => event[@source],
                   :event => event)
      return
    end

    raw = event[@source].first
    begin
      values = CSV.parse_line(raw, :col_sep => @separator, :quote_char => @quote_char)

      if @target.nil?
        # Default is to write to the root of the event.
        dest = event
      else
        dest = event[@target] ||= {}
      end

      values.each_index do |i|
        field_name = @columns[i] || "column#{i+1}"
        dest[field_name] = values[i]
      end

      filter_matched(event)
    rescue => e
      event.tag "_csvparsefailure"
      @logger.warn("Trouble parsing csv", :source => @source, :raw => raw,
                    :exception => e)
      return
    end # begin
  end # if event

  @logger.debug("Event after csv filter", :event => event)

end

#register ⇒ Object



40
41
42
43
44
# File 'lib/logstash/filters/csv.rb', line 40

def register

  # Nothing to do here

end