mirror of
https://github.com/meineerde/rackstash.git
synced 2025-10-17 14:01:01 +00:00
Add Rackstash::Buffer#buffering? flag
Generally, a non-buffering Buffer will eventually be flushed to the sink after each logged message. This thus mostly resembles the way traditional loggers work in Ruby. A buffering Buffer however holds log messages, fields and tags for a longer time. Only at a specific time, all log messages and stored fields will be flushed to the Sink as a single log event. A common scope for such an event is a full request to a Rack app.
This commit is contained in:
parent
5e37df77e5
commit
bfe4cc854a
@ -28,13 +28,17 @@ module Rackstash
|
||||
# current buffer. It contains frozen strings only.
|
||||
attr_reader :tags
|
||||
|
||||
# @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}.
|
||||
# @param allow_empty [Boolean] When set to `true` 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.
|
||||
def initialize(allow_empty: false)
|
||||
def initialize(buffering: true, allow_empty: false)
|
||||
@buffering = !!buffering
|
||||
@allow_empty = !!allow_empty
|
||||
|
||||
@messages = []
|
||||
@ -60,6 +64,16 @@ module Rackstash
|
||||
@allow_empty
|
||||
end
|
||||
|
||||
# When set to `true` in {#initialize}, 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}.
|
||||
#
|
||||
# @return [Boolean] true if the current buffer is intended to hold buffered
|
||||
# data of multiple log calls
|
||||
def buffering?
|
||||
@buffering
|
||||
end
|
||||
|
||||
# Clear the current buffer from all stored data, just as it was right after
|
||||
# inititialization.
|
||||
#
|
||||
|
||||
@ -34,6 +34,17 @@ describe Rackstash::Buffer do
|
||||
end
|
||||
end
|
||||
|
||||
describe '#buffering?' do
|
||||
it 'defaults to false' do
|
||||
expect(buffer.buffering?).to be true
|
||||
end
|
||||
|
||||
it 'can be overwritten in initialize' do
|
||||
buffer_options[:buffering] = false
|
||||
expect(buffer.buffering?).to be false
|
||||
end
|
||||
end
|
||||
|
||||
describe '#clear' do
|
||||
it 'removes all fields and tags' do
|
||||
buffer.fields['foo'] = 'bar'
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user