diff --git a/lib/rackstash/encoders/message.rb b/lib/rackstash/encoders/message.rb new file mode 100644 index 0000000..f7615c7 --- /dev/null +++ b/lib/rackstash/encoders/message.rb @@ -0,0 +1,23 @@ +# 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. + +module Rackstash + module Encoders + # The Message encoder only returns the message of log event. All other + # fields and tags are ignored. + # + # This encoder is useful in environments where the added fields are not + # required, mostly during development where debug logs are directly consumed + # by humans + class Message + # @param event [Hash] a log event as produced by the {Flow} + # @return [String] the `"message"` field of the event. Trailing whitespace + # will be removed. + def encode(event) + event[FIELD_MESSAGE].rstrip + end + end + end +end diff --git a/spec/rackstash/encoders/message_spec.rb b/spec/rackstash/encoders/message_spec.rb new file mode 100644 index 0000000..d28a75f --- /dev/null +++ b/spec/rackstash/encoders/message_spec.rb @@ -0,0 +1,24 @@ +# 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/message' + +describe Rackstash::Encoders::Message do + let(:encoder) { Rackstash::Encoders::Message.new } + + describe '#encode' do + it 'gets the message from the event hash' do + event = { 'hello' => 'world', 'message' => 'hello' } + expect(encoder.encode(event)).to eql 'hello' + end + + it 'rstrips the message' do + event = { 'message' => "\n\t \nline1\nline2\n \n\t\n" } + expect(encoder.encode(event)).to eql "\n\t \nline1\nline2" + end + end +end