From 7c0c983dc2b449dc65b3ff483591e7a5a920459f Mon Sep 17 00:00:00 2001 From: Holger Just Date: Mon, 19 Jun 2017 10:28:28 +0200 Subject: [PATCH] Add Message encoder to just log the log message This encoder is useful for local consumption of the raw log stream, e.g. during development where the developer might not care for any additional fields. With this encoder, the log output can mostly resemble a "classic" line-based log feed. --- lib/rackstash/encoders/message.rb | 23 +++++++++++++++++++++++ spec/rackstash/encoders/message_spec.rb | 24 ++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 lib/rackstash/encoders/message.rb create mode 100644 spec/rackstash/encoders/message_spec.rb 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