require "logstash/codecs/base"
require "logstash/util/charset"
require "zlib"
require "stringio"
class LogStash::Codecs::GZIP < LogStash::Codecs::Base
config_name "gzip"
config :charset, :validate => ::Encoding.name_list, :default => "UTF-8"
MESSAGE_FIELD = "message".freeze
public
def register
@converter = LogStash::Util::Charset.new(@charset)
@converter.logger = @logger
end
public
def decode(data)
begin
decoded = Zlib::GzipReader.new(StringIO.new(data)).read
yield LogStash::Event.new(MESSAGE_FIELD => @converter.convert(decoded))
rescue Zlib::Error, Zlib::GzipFile::Error=> e
@logger.info? && @logger.info("Gzip codec: GZIP parse failure. Falling back to plain-text", :error => e, :data => data)
yield LogStash::Event.new(MESSAGE_FIELD => @converter.convert(data), "tags" => ["_gzipparsefailure"])
end
end
end