1
0
mirror of https://github.com/meineerde/rackstash.git synced 2025-10-17 14:01:01 +00:00

Omit an empty message when encoding an event as JSON

This commit is contained in:
Holger Just 2018-01-27 00:32:02 +01:00
parent db57d21a45
commit d6cc062b93
2 changed files with 13 additions and 3 deletions

View File

@ -1,6 +1,6 @@
# frozen_string_literal: true
#
# Copyright 2017 Holger Just
# Copyright 2017 - 2018 Holger Just
#
# This software may be modified and distributed under the terms
# of the MIT license. See the LICENSE.txt file for details.
@ -28,7 +28,7 @@ module Rackstash
# @param event [Hash] a log event as produced by the {Flow}
# @return [String] the event as a single-line JSON string
def encode(event)
normalize_message(event)
normalize_message(event) unless event[FIELD_MESSAGE].nil?
normalize_timestamp(event)
::JSON.dump(event)

View File

@ -1,6 +1,6 @@
# frozen_string_literal: true
#
# Copyright 2017 Holger Just
# Copyright 2017 - 2018 Holger Just
#
# This software may be modified and distributed under the terms
# of the MIT license. See the LICENSE.txt file for details.
@ -22,5 +22,15 @@ describe Rackstash::Encoder::JSON do
event = { 'message' => "text\nwith\nnewlines" }
expect(encoder.encode(event)).to eql '{"message":"text\nwith\nnewlines"}'
end
it 'passes the message as nil' do
event = { 'message' => nil, 'foo' => 'bar' }
expect(encoder.encode(event)).to eql '{"message":null,"foo":"bar"}'
end
it 'omits a missing message' do
event = { 'foo' => 'bar' }
expect(encoder.encode(event)).to eql '{"foo":"bar"}'
end
end
end