Class: LogStash::Codecs::OldLogStashJSON

Inherits:
Base
  • Object
show all
Defined in:
lib/logstash/codecs/oldlogstashjson.rb

Constant Summary collapse

V0_TO_V1 =

Map from v0 name to v1 name. Note: @source is gone and has no similar field.

{"@timestamp" => "@timestamp", "@message" => "message",
"@tags" => "tags", "@type" => "type",
"@source_host" => "host", "@source_path" => "path"}

Constants included from LogStash::Config::Mixin

LogStash::Config::Mixin::CONFIGSORT

Instance Attribute Summary

Attributes included from LogStash::Config::Mixin

#config, #original_params

Attributes inherited from Plugin

#logger, #params

Instance Method Summary collapse

Methods inherited from Base

#clone, #flush, #initialize, #on_event, #teardown

Methods included from LogStash::Config::Mixin

#config_init, included

Methods inherited from Plugin

#eql?, #finished, #finished?, #hash, #initialize, #inspect, lookup, #reload, #running?, #shutdown, #teardown, #terminating?, #to_s

Constructor Details

This class inherits a constructor from LogStash::Codecs::Base

Instance Method Details

#decode(data) {|LogStash::Event.new(h)| ... } ⇒ Object

Yields:



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/logstash/codecs/oldlogstashjson.rb', line 15

def decode(data)
  begin
    obj = JSON.parse(data.force_encoding("UTF-8"))
  rescue JSON::ParserError => e
    @logger.info("JSON parse failure. Falling back to plain-text", :error => e, :data => data)
    yield LogStash::Event.new("message" => data)
    return
  end

  h  = {}

  # Convert the old logstash schema to the new one.
  V0_TO_V1.each do |key, val|
    h[val] = obj[key] if obj.include?(key)
  end

  h.merge!(obj["@fields"]) if obj["@fields"].is_a?(Hash)
  yield LogStash::Event.new(h)
end

#encode(data) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/logstash/codecs/oldlogstashjson.rb', line 36

def encode(data)
  h  = {}

  # Convert the new logstash schema to the old one.
  V0_TO_V1.each do |key, val|
    h[key] = data[val] if data.include?(val)
  end

  data.to_hash.each do |field, val|
    # TODO: might be better to V1_TO_V0 = V0_TO_V1.invert during
    # initialization than V0_TO_V1.has_value? within loop
    next if field == "@version" or V0_TO_V1.has_value?(field)
    h["@fields"] = {} if h["@fields"].nil?
    h["@fields"][field] = val
  end

  # Tack on a \n because JSON outputs 1.1.x had them.
  @on_event.call(h.to_json + "\n")
end