Class: LogStash::Codecs::GzipJson

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

Overview

This codec will read gzip encoded json content

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ GzipJson

Returns a new instance of GzipJson.



25
26
27
28
29
# File 'lib/logstash/codecs/gzip_json.rb', line 25

def initialize(params={})
  super(params)
  @converter = LogStash::Util::Charset.new(@charset)
  @converter.logger = @logger
end

Instance Method Details

#decode(data) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/logstash/codecs/gzip_json.rb', line 32

def decode(data)
  begin
    @decoder = Zlib::GzipReader.new(StringIO.new(data))
    json_string = @decoder.read    
  rescue Zlib::Error, Zlib::GzipFile::Error=> e     
    @logger.error("Gzip codec: We cannot uncompress the gzip file", :error => e, :data => data)
    raise e
  end    

  begin
    yield LogStash::Event.new(JSON.parse(json_string))
  rescue JSON::ParserError => e
    @logger.info('JSON parse failure. Falling back to plain-text', :error => e, :data => json_string)
    yield LogStash::Event.new('message' => json_string)
  end
  
end