diff --git a/lib/rackstash/adapters/callable.rb b/lib/rackstash/adapters/callable.rb index ceb3db2..935e4d5 100644 --- a/lib/rackstash/adapters/callable.rb +++ b/lib/rackstash/adapters/callable.rb @@ -6,7 +6,7 @@ # of the MIT license. See the LICENSE.txt file for details. require 'rackstash/adapters/adapter' -require 'rackstash/encoders/raw' +require 'rackstash/encoders/hash' module Rackstash module Adapters @@ -46,15 +46,15 @@ module Rackstash end end - # By default, we use an {Rackstash::Encoders::Raw} to encode the events. - # This ensures that the raw event is passed through to to the callable by - # default. + # By default, we use an {Rackstash::Encoders::Hash} to encode the events. + # This ensures that all of the data in the logged event is passed through + # to the callable by default. # # You can define a custom encoder in the responsible {Flow}. # - # @return [Rackstash::Encoders::Raw] a new Raw encoder + # @return [Rackstash::Encoders::Hash] a new Hash encoder def default_encoder - Rackstash::Encoders::Raw.new + Rackstash::Encoders::Hash.new end # Write a single log line by calling the defined `callable` given in diff --git a/lib/rackstash/encoders.rb b/lib/rackstash/encoders.rb index 03286a5..485a8f6 100644 --- a/lib/rackstash/encoders.rb +++ b/lib/rackstash/encoders.rb @@ -5,6 +5,7 @@ # This software may be modified and distributed under the terms # of the MIT license. See the LICENSE.txt file for details. +require 'rackstash/encoders/hash' require 'rackstash/encoders/json' require 'rackstash/encoders/lograge' require 'rackstash/encoders/logstash' diff --git a/lib/rackstash/encoders/hash.rb b/lib/rackstash/encoders/hash.rb new file mode 100644 index 0000000..5811228 --- /dev/null +++ b/lib/rackstash/encoders/hash.rb @@ -0,0 +1,30 @@ +# 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/helpers/message' +require 'rackstash/encoders/helpers/timestamp' + +module Rackstash + module Encoders + # The Hash encoder formats the log event as a raw `Hash` containing all data + # exposed by the buffer. This can be used by special log targets which are + # designed to handle hashes as opposed to formatted strings. + class Hash + include Rackstash::Encoders::Helpers::Message + include Rackstash::Encoders::Helpers::Timestamp + + # @param event [Hash] a log event as produced by the {Flow} + # @return [Hash] the normalized event + def encode(event) + normalize_message(event) + normalize_timestamp(event) + + event + end + end + end +end diff --git a/lib/rackstash/encoders/lograge.rb b/lib/rackstash/encoders/lograge.rb index 54bf8fc..47f77da 100644 --- a/lib/rackstash/encoders/lograge.rb +++ b/lib/rackstash/encoders/lograge.rb @@ -123,10 +123,10 @@ module Rackstash case value when nil return - when Hash + when ::Hash return if value.empty? return serialize_hash(value, prefix: key) - when Array + when ::Array return if value.empty? return serialize_array(value, prefix: key) when Float diff --git a/lib/rackstash/encoders/message.rb b/lib/rackstash/encoders/message.rb index 5802976..ef9a127 100644 --- a/lib/rackstash/encoders/message.rb +++ b/lib/rackstash/encoders/message.rb @@ -57,7 +57,7 @@ module Rackstash case value when nil nil - when Array + when ::Array "[#{value.map(&:to_s).join(',')}] " else "[#{value}] " diff --git a/lib/rackstash/encoders/raw.rb b/lib/rackstash/encoders/raw.rb index dd0a332..2f53188 100644 --- a/lib/rackstash/encoders/raw.rb +++ b/lib/rackstash/encoders/raw.rb @@ -7,9 +7,12 @@ module Rackstash module Encoders - # The Raw encoder formats the log event as a raw `Hash` containing all data - # exposed by the buffer. This can be used by special log targets which are - # designed to handle hashes as opposed to formatted strings. + # The Raw encoder passes along the raw unformatted event hash. It still + # contains an `Array` of {Message} objects in the `"message"` key and a + # `Time` object in the `"@timestamp"` key. + # + # When expecting a Hash in an adapter, usually it's more useful to use the + # {Rackstash::Encoders::Hash} encoder instead. class Raw # @param event [Hash] a log event as produced by the {Flow} # @return [Hash] the passed `event` diff --git a/spec/rackstash/adapters/callable_spec.rb b/spec/rackstash/adapters/callable_spec.rb index 87ed82b..40b8886 100644 --- a/spec/rackstash/adapters/callable_spec.rb +++ b/spec/rackstash/adapters/callable_spec.rb @@ -32,8 +32,8 @@ describe Rackstash::Adapters::Callable do end describe '.default_encoder' do - it 'returns a Raw encoder' do - expect(adapter.default_encoder).to be_instance_of Rackstash::Encoders::Raw + it 'returns a Hash encoder' do + expect(adapter.default_encoder).to be_instance_of Rackstash::Encoders::Hash end end diff --git a/spec/rackstash/encoders/hash_spec.rb b/spec/rackstash/encoders/hash_spec.rb new file mode 100644 index 0000000..ec2fbaf --- /dev/null +++ b/spec/rackstash/encoders/hash_spec.rb @@ -0,0 +1,33 @@ +# 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/hash' + +describe Rackstash::Encoders::Hash do + let(:encoder) { described_class.new } + + describe '#encode' do + it 'normalized the message' do + event = { 'message' => ["hello\n", "world\n", "foo", "bar"] } + expect(encoder.encode(event)).to eql 'message' => "hello\nworld\nfoobar" + end + + it 'normalizes the timestamp' do + time = Time.now + event = { 'message' => ['foo', 'bar'], '@timestamp' => time } + + expect(encoder.encode(event)).to eql 'message' => 'foobar', '@timestamp' => time.getutc.iso8601(6) + end + + it 'passes the normalized event hash through' do + event = { 'foo' => 'bar', 'baz' => :boing } + expect(encoder.encode(event)).to eql 'foo' => 'bar', 'baz' => :boing, 'message' => '' + end + end +end