From c1e3c99ab5582595f10e1d81b94de3c3539bca29 Mon Sep 17 00:00:00 2001 From: Holger Just Date: Thu, 19 Oct 2017 23:04:48 +0200 Subject: [PATCH] Do not write empty lines to IO and File adapters If a line is completely empty, it is not useful to log it at all since it would be invisible on the created logstream anyway. If we get passed an empty String from the encoder, we can thus silently ignore it. --- lib/rackstash/adapters/adapter.rb | 9 +++++---- lib/rackstash/adapters/file.rb | 5 ++++- lib/rackstash/adapters/io.rb | 5 ++++- spec/rackstash/adapters/file_spec.rb | 7 +++++++ spec/rackstash/adapters/io_spec.rb | 6 ++++++ 5 files changed, 26 insertions(+), 6 deletions(-) diff --git a/lib/rackstash/adapters/adapter.rb b/lib/rackstash/adapters/adapter.rb index de94e75..4b59e61 100644 --- a/lib/rackstash/adapters/adapter.rb +++ b/lib/rackstash/adapters/adapter.rb @@ -96,16 +96,17 @@ module Rackstash private - # Helper method to ensure that a log line passed to {#write} is a string - # that ends in a newline character by mutating the object is required. + # Helper method to ensure that a log line passed to {#write} is either a + # String that ends in a newline character or is completely empty. # # @param line [#to_s] a log line # @return [String] `line` with a trailing newline character (`"\n"`) # appended if necessary def normalize_line(line) line = line.to_s - line = "#{line}\n" unless line.end_with?("\n".freeze) - line + return line if line.empty? || line.end_with?("\n".freeze) + + "#{line}\n" end end end diff --git a/lib/rackstash/adapters/file.rb b/lib/rackstash/adapters/file.rb index 5ddd8ee..9890fa3 100644 --- a/lib/rackstash/adapters/file.rb +++ b/lib/rackstash/adapters/file.rb @@ -104,9 +104,12 @@ module Rackstash # @param log [#to_s] the encoded log event # @return [nil] def write_single(log) + line = normalize_line(log) + return if line.empty? + @mutex.synchronize do auto_reopen - @file.write normalize_line(log) + @file.write(line) end nil end diff --git a/lib/rackstash/adapters/io.rb b/lib/rackstash/adapters/io.rb index c6d1bb0..dbd44fa 100644 --- a/lib/rackstash/adapters/io.rb +++ b/lib/rackstash/adapters/io.rb @@ -88,8 +88,11 @@ module Rackstash # @param log [#to_s] the encoded log event # @return [nil] def write_single(log) + line = normalize_line(log) + return if line.empty? + @io.synchronize_for_rackstash do - @io.write normalize_line(log) + @io.write line @io.flush if @flush_immediately end nil diff --git a/spec/rackstash/adapters/file_spec.rb b/spec/rackstash/adapters/file_spec.rb index a1a9531..248b23e 100644 --- a/spec/rackstash/adapters/file_spec.rb +++ b/spec/rackstash/adapters/file_spec.rb @@ -92,6 +92,13 @@ describe Rackstash::Adapters::File do expect(logfile.tap(&:rewind).read).to eql "a full line.\n" end + it 'ignores empty log lines' do + adapter.write('') + adapter.write(nil) + + expect(logfile.tap(&:rewind).read).to eql '' + end + context 'with auto_reopen: true' do let(:adapter_args) { { auto_reopen: true } } diff --git a/spec/rackstash/adapters/io_spec.rb b/spec/rackstash/adapters/io_spec.rb index 074c243..ec67f24 100644 --- a/spec/rackstash/adapters/io_spec.rb +++ b/spec/rackstash/adapters/io_spec.rb @@ -77,5 +77,11 @@ describe Rackstash::Adapters::IO do adapter.write('bar') end end + + it 'ignores empty log lines' do + expect(io).to_not receive(:write) + adapter.write('') + adapter.write(nil) + end end end