Class: LogStash::Filters::Multiline

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

Instance Attribute Summary

Attributes inherited from Base

#logger

Instance Method Summary collapse

Methods inherited from Base

#add_config

Constructor Details

#initialize(config = {}) ⇒ Multiline

Returns a new instance of Multiline.



56
57
58
59
60
61
# File 'lib/logstash/filters/multiline.rb', line 56

def initialize(config = {})
  super

  @types = Hash.new { |h,k| h[k] = [] }
  @pending = Hash.new
end

Instance Method Details

#filter(event) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/logstash/filters/multiline.rb', line 97

def filter(event)
  return unless @types.member?(event.type)
  typeconfig = @types[event.type]
  match = typeconfig["pattern"].match(event.message)
  key = [event.source, event.type]
  pending = @pending[key]

  @logger.debug(["Reg: ", typeconfig["pattern"], event.message, match, typeconfig["negate"]])
  # Add negate option
  match = (match and !typeconfig["negate"]) || (!match and typeconfig["negate"])

  case typeconfig["what"]
  when "previous"
    if match
      event.tags |= ["multiline"]
      # previous previous line is part of this event.
      # append it to the event and cancel it
      if pending
        pending.append(event)
      else
        @pending[key] = event
      end
      event.cancel
    else
      # this line is not part of the previous event
      # if we have a pending event, it's done, send it.
      # put the current event into pending
      if pending
        tmp = event.to_hash
        event.overwrite(pending)
        @pending[key] = LogStash::Event.new(tmp)
      else
        @pending[key] = event
        event.cancel
      end # if/else pending
    end # if/else match
  when "next"
    if match
      event.tags |= ["multiline"]
      # this line is part of a multiline event, the next
      # line will be part, too, put it into pending.
      if pending
        pending.append(event)
      else
        @pending[key] = event
      end
      event.cancel
    else
      # if we have something in pending, join it with this message
      # and send it. otherwise, this is a new message and not part of
      # multiline, send it.
      if pending
        pending.append(event)
        event.overwrite(pending.to_hash)
        @pending.delete(key)
      end
    end # if/else match
  else
    @logger.warn(["Unknown multiline 'what' value.", typeconfig])
  end # case typeconfig["what"]
  #end # @types[event.type].each
end

#flush(source, type) ⇒ Object



162
163
164
165
166
167
168
169
# File 'lib/logstash/filters/multiline.rb', line 162

def flush(source, type)
  key = [source, type]
  if @pending[key]
    event = @pending[key]
    @pending.delete(key)
  end
  return event
end

#registerObject



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
94
# File 'lib/logstash/filters/multiline.rb', line 64

def register
  @config.each do |type, typeconfig|
    # typeconfig will be a hash containing 'pattern' and 'what'
    @logger.debug "Setting type #{type.inspect} to the config #{typeconfig.inspect}"
    raise "type \"#{type}\" defined more than once" unless @types[type].empty?
    @types[type] = typeconfig

    if !typeconfig.include?("pattern")
      @logger.fatal(["'multiline' filter config for type #{type} is missing" \
                     " 'pattern' setting", typeconfig])
    end

    if !typeconfig.include?("what")
      @logger.fatal(["'multiline' filter config for type #{type} is missing" \
                     " 'what' setting", typeconfig])
    end

    if !["next", "previous"].include?(typeconfig["what"])
      @logger.fatal(["'multiline' filter config for type #{type} has " \
                     "invalid 'what' value. Must be 'next' or 'previous'",
                     typeconfig])
    end

    begin
      typeconfig["pattern"] = Regexp.new(typeconfig["pattern"])
    rescue RegexpError => e
      @logger.fatal(["Invalid pattern for multiline filter on type '#{type}'",
                    typeconfig, e])
    end
  end # @config.each
end