Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
sanger_warren (0.5.1)
sanger_warren (0.5.2)
bunny (~> 2.17)
connection_pool (~> 2.2)
multi_json (>= 1.0, < 1.18.0)
Expand Down
8 changes: 7 additions & 1 deletion lib/warren/log_tagger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,14 @@ def self.level(name)

private

# Applies the tag to the message, ensuring it is valid UTF-8 and safe for logging.
#
# @param message [Object] The message to tag
# @return [String] The tagged message
def tag(message)
"#{@tag}: #{message}"
msg = message.to_s.dup.force_encoding('BINARY') # handle any encoding
msg = msg.encode('UTF-8', invalid: :replace, undef: :replace, replace: '?')
"#{@tag}: #{msg}"
end
end
end
2 changes: 1 addition & 1 deletion lib/warren/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

module Warren
# Gem version number. Bump prior to release of new version
VERSION = '0.5.1'
VERSION = '0.5.2'
end
25 changes: 21 additions & 4 deletions spec/warren/log_tagger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,41 @@
allow(standard_logger).to receive(:error) { |&block| block&.call }
end

shared_examples 'a logger' do |method|
shared_examples 'a logger' do |method| # rubocop:disable Metrics/BlockLength
let(:program) { 'program' }
let(:message) { 'message' }

context 'without a block' do
before { logger.send(method, 'message') }
before { logger.send(method, message) }

it 'tags the message' do
expect(standard_logger).to have_received(method).with('tag: message')
end

context 'with invalid UTF-8 bytes' do
let(:message) { "message\xFF".dup.force_encoding('ASCII-8BIT') }

it 'tags the message, replacing invalid bytes' do
expect(standard_logger).to have_received(method).with('tag: message?')
end
end
end

context 'with a block' do
subject(:method_call) { logger.send(method, 'program') { 'message' } }
subject(:method_call) { logger.send(method, program) { message } }

it 'calls the logger as normal' do
method_call
expect(standard_logger).to have_received(method).with('program')
expect(standard_logger).to have_received(method).with(program)
end

it { is_expected.to eq 'tag: message' }

context 'with invalid UTF-8 bytes' do
let(:message) { "message\xFF".dup.force_encoding('ASCII-8BIT') }

it { is_expected.to eq 'tag: message?' }
end
end
end

Expand Down