From 95c8b8f8fb6c4a616a1858826341a61b21848caa Mon Sep 17 00:00:00 2001 From: Holger Just Date: Fri, 3 Feb 2017 18:43:34 +0100 Subject: [PATCH] Pass the sink from the Logger to each individual Buffer --- lib/rackstash/buffer.rb | 13 ++++++++++++- lib/rackstash/buffer_stack.rb | 9 ++++++++- lib/rackstash/logger.rb | 2 +- spec/rackstash/buffer_spec.rb | 3 ++- spec/rackstash/buffer_stack_spec.rb | 4 +++- 5 files changed, 26 insertions(+), 5 deletions(-) diff --git a/lib/rackstash/buffer.rb b/lib/rackstash/buffer.rb index 9a18d8a..7b14a59 100644 --- a/lib/rackstash/buffer.rb +++ b/lib/rackstash/buffer.rb @@ -28,6 +28,9 @@ module Rackstash # current buffer. It contains frozen strings only. attr_reader :tags + # @return [Sink] the log sink where the buffer is eventually flushed to + attr_reader :sink + # @param buffering [Boolean] When set to `true`, this buffer is considered # to be buffering data. When buffering, logged messages will not be # flushed immediately but only with an explicit call to {#flush}. @@ -37,7 +40,8 @@ module Rackstash # explicit changes to the buffer (e.g. a logged message, added tags or # fields), the buffer will not be flushed to the sink but will be silently # dropped. - def initialize(buffering: true, allow_empty: false) + def initialize(sink, buffering: true, allow_empty: false) + @sink = sink @buffering = !!buffering @allow_empty = !!allow_empty @@ -60,6 +64,13 @@ module Rackstash message end + # When set to `true` in {#initialize}, the data in this buffer will be + # flushed to the sink, even if no messages were logged but there were just + # added fields or tags. If this is `false` and there were no explicit + # changes to the buffer (e.g. a logged message, added tags or fields), the + # buffer will not be flushed to the sink but will be silently dropped. + # + # @return [Boolean] def allow_empty? @allow_empty end diff --git a/lib/rackstash/buffer_stack.rb b/lib/rackstash/buffer_stack.rb index c48a506..626d30a 100644 --- a/lib/rackstash/buffer_stack.rb +++ b/lib/rackstash/buffer_stack.rb @@ -11,9 +11,16 @@ module Rackstash # is used by exactly one BufferedLogger. The responsible {BufferedLogger} # ensures that each BufferStack is only accessed from a single `Thread`. class BufferStack + # @return [Sink] the log sink where the buffers are eventually flushed to + attr_reader :sink + + def initialize(sink) + @sink = sink + end + # TODO: this is only a spike for now def with_buffer - yield Buffer.new + yield Buffer.new(@sink) end end end diff --git a/lib/rackstash/logger.rb b/lib/rackstash/logger.rb index 1bf6e3b..1eb027f 100644 --- a/lib/rackstash/logger.rb +++ b/lib/rackstash/logger.rb @@ -242,7 +242,7 @@ module Rackstash private def buffer_stack - @buffer_stack ||= Rackstash::BufferStack.new + @buffer_stack ||= Rackstash::BufferStack.new(@sink) end end end diff --git a/spec/rackstash/buffer_spec.rb b/spec/rackstash/buffer_spec.rb index 3053ef7..63aedda 100644 --- a/spec/rackstash/buffer_spec.rb +++ b/spec/rackstash/buffer_spec.rb @@ -9,7 +9,8 @@ require 'rackstash/buffer' describe Rackstash::Buffer do let(:buffer_options) { {} } - let(:buffer) { Rackstash::Buffer.new(**buffer_options) } + let(:sink) { instance_double(Rackstash::Sink) } + let(:buffer) { Rackstash::Buffer.new(sink, **buffer_options) } describe '#allow_empty?' do it 'defaults to false' do diff --git a/spec/rackstash/buffer_stack_spec.rb b/spec/rackstash/buffer_stack_spec.rb index 8526349..0d89245 100644 --- a/spec/rackstash/buffer_stack_spec.rb +++ b/spec/rackstash/buffer_stack_spec.rb @@ -8,12 +8,14 @@ require 'spec_helper' require 'rackstash/buffer_stack' describe Rackstash::BufferStack do - let(:stack) { Rackstash::BufferStack.new } + let(:sink) { instance_double(Rackstash::Sink) } + let(:stack) { Rackstash::BufferStack.new(sink) } describe '#with_buffer' do it 'initializes a buffer' do stack.with_buffer do |buffer| expect(buffer).to be_a Rackstash::Buffer + expect(buffer.sink).to equal sink end end end