diff --git a/lib/rackstash/encoders/logstash.rb b/lib/rackstash/encoders/logstash.rb new file mode 100644 index 0000000..d3e2c4b --- /dev/null +++ b/lib/rackstash/encoders/logstash.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true +# Copyright 2017 Holger Just +# +# This software may be modified and distributed under the terms +# of the MIT license. See the LICENSE.txt file for details. + +require 'rackstash/encoders/json' + +module Rackstash + module Encoders + # The JSON encoder formats the log event as a single-line JSON string. The + # resulting JSON string contains all data exposed by the buffer. Leading + # and trailing whitespace as well as any ANSI color codes in the `"message"` + # field will be removed. + # + # The resulting string is in the JSON format native to Logstash. You can + # thus ship your logs directly to Logstash without further processing by + # using Logstash's [json codec](https://www.elastic.co/guide/en/logstash/current/plugins-codecs-json.html) + # on the input definition. + class Logstash < JSON + # @param event [Hash] a log event as produced by the {Flow} + # @return [String] the event as a single-line JSON string + def encode(event) + event[FIELD_VERSION] = '1'.freeze if event[FIELD_VERSION].nil? + super(event) + end + end + end +end diff --git a/spec/rackstash/encoders/logstash_spec.rb b/spec/rackstash/encoders/logstash_spec.rb new file mode 100644 index 0000000..28eebe4 --- /dev/null +++ b/spec/rackstash/encoders/logstash_spec.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true +# Copyright 2017 Holger Just +# +# This software may be modified and distributed under the terms +# of the MIT license. See the LICENSE.txt file for details. + +require 'spec_helper' + +require 'rackstash/encoders/logstash' + +describe Rackstash::Encoders::Logstash do + let(:encoder) { described_class.new } + + describe '#encode' do + it 'formats the passed event hash as a JSON string and includes @version' do + event = { 'hello' => 'world', 'message' => 'hello' } + expect(encoder.encode(event)).to eql '{"hello":"world","message":"hello","@version":"1"}' + end + end +end