1
0
mirror of https://github.com/meineerde/rackstash.git synced 2026-01-31 17:27:13 +00:00

Pass the sink from the Logger to each individual Buffer

This commit is contained in:
Holger Just 2017-02-03 18:43:34 +01:00
parent bfe4cc854a
commit 95c8b8f8fb
5 changed files with 26 additions and 5 deletions

View File

@ -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

View File

@ -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

View File

@ -242,7 +242,7 @@ module Rackstash
private
def buffer_stack
@buffer_stack ||= Rackstash::BufferStack.new
@buffer_stack ||= Rackstash::BufferStack.new(@sink)
end
end
end

View File

@ -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

View File

@ -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