Skip to content

Commit 9cf58d2

Browse files
authored
Merge pull request #55 from sanger/fix-logger-tagger-incompatible-character-encodings
Fix logger tagger incompatible character encodings
2 parents fcee96d + cb1b619 commit 9cf58d2

4 files changed

Lines changed: 30 additions & 7 deletions

File tree

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
sanger_warren (0.5.1)
4+
sanger_warren (0.5.2)
55
bunny (~> 2.17)
66
connection_pool (~> 2.2)
77
multi_json (>= 1.0, < 1.18.0)

lib/warren/log_tagger.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,14 @@ def self.level(name)
5151

5252
private
5353

54+
# Applies the tag to the message, ensuring it is valid UTF-8 and safe for logging.
55+
#
56+
# @param message [Object] The message to tag
57+
# @return [String] The tagged message
5458
def tag(message)
55-
"#{@tag}: #{message}"
59+
msg = message.to_s.dup.force_encoding('BINARY') # handle any encoding
60+
msg = msg.encode('UTF-8', invalid: :replace, undef: :replace, replace: '?')
61+
"#{@tag}: #{msg}"
5662
end
5763
end
5864
end

lib/warren/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
module Warren
44
# Gem version number. Bump prior to release of new version
5-
VERSION = '0.5.1'
5+
VERSION = '0.5.2'
66
end

spec/warren/log_tagger_spec.rb

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,41 @@
1616
allow(standard_logger).to receive(:error) { |&block| block&.call }
1717
end
1818

19-
shared_examples 'a logger' do |method|
19+
shared_examples 'a logger' do |method| # rubocop:disable Metrics/BlockLength
20+
let(:program) { 'program' }
21+
let(:message) { 'message' }
22+
2023
context 'without a block' do
21-
before { logger.send(method, 'message') }
24+
before { logger.send(method, message) }
2225

2326
it 'tags the message' do
2427
expect(standard_logger).to have_received(method).with('tag: message')
2528
end
29+
30+
context 'with invalid UTF-8 bytes' do
31+
let(:message) { "message\xFF".dup.force_encoding('ASCII-8BIT') }
32+
33+
it 'tags the message, replacing invalid bytes' do
34+
expect(standard_logger).to have_received(method).with('tag: message?')
35+
end
36+
end
2637
end
2738

2839
context 'with a block' do
29-
subject(:method_call) { logger.send(method, 'program') { 'message' } }
40+
subject(:method_call) { logger.send(method, program) { message } }
3041

3142
it 'calls the logger as normal' do
3243
method_call
33-
expect(standard_logger).to have_received(method).with('program')
44+
expect(standard_logger).to have_received(method).with(program)
3445
end
3546

3647
it { is_expected.to eq 'tag: message' }
48+
49+
context 'with invalid UTF-8 bytes' do
50+
let(:message) { "message\xFF".dup.force_encoding('ASCII-8BIT') }
51+
52+
it { is_expected.to eq 'tag: message?' }
53+
end
3754
end
3855
end
3956

0 commit comments

Comments
 (0)