Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gracefully handle json Encoding::UndefinedConversionError #165

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
Only update the message encoding
  • Loading branch information
marcoadkins committed Sep 16, 2020
commit cf16de8677bd88778e980f10b765e1840350e9a3
8 changes: 3 additions & 5 deletions lib/logstash-logger/formatter/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,9 @@ def format_event(event)
end

def force_utf8_encoding(event)
new_event = event.dup
new_data = {}
new_event.instance_variable_get(:@data).each { |k, v| new_data[k] = v.to_s.force_encoding('UTF-8') }
new_event.instance_variable_set(:@data, new_data)
new_event
original_message = event.instance_variable_get(:@data)['message']
event.message = original_message.force_encoding('UTF-8')
event
end
end
end
Expand Down
13 changes: 6 additions & 7 deletions spec/formatter/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,14 @@
describe '#force_utf8_encoding' do
let(:event) { LogStash::Event.new("message" => "foo".force_encoding('ASCII-8BIT')) }

it 'returns a new event' do
expect(subject.send(:force_utf8_encoding, event)).not_to eq(event)
it 'returns the same event' do
expect(subject.send(:force_utf8_encoding, event)).to eq(event)
end

it 'forces all event data to UTF-8 encoding' do
updated_event_data = subject.send(:force_utf8_encoding, event).instance_variable_get(:@data)
updated_event_data.each_value do |value|
expect(value.encoding.name).to eq('UTF-8')
end
it 'forces the event message to UTF-8 encoding' do
subject.send(:force_utf8_encoding, event)
updated_event_data = event.instance_variable_get(:@data)
expect(updated_event_data['message'].encoding.name).to eq('UTF-8')
end
end

Expand Down