1
0
mirror of https://github.com/meineerde/rackstash.git synced 2025-10-17 14:01:01 +00:00

Add Logger#<< to ad a raw unformatted message to the buffer

If the current Buffer is bufering, the message will just be added. Else,
it will be flushed to the sink directly.
This commit is contained in:
Holger Just 2017-02-09 14:13:05 +01:00
parent 031198cf14
commit 68a2b57e28
3 changed files with 25 additions and 3 deletions

View File

@ -56,6 +56,21 @@ module Rackstash
@buffer_stack = Concurrent::ThreadLocalVar.new
end
# Add a message to the current buffer without any further formatting. If
# the current {Buffer} is bufering, the message will just be added. Else,
# it will be flushed to the {#sink} directly.
#
# @param msg [Object]
# @return [String] the passed `msg`
def <<(msg)
buffer.add_message Message.new(
msg,
time: Time.now.utc.freeze,
)
msg
end
# Set the base log level as either one of the {SEVERITIES} or a
# String/Symbol describing the level. When logging a message, it will only
# be added if its log level is at or above the base level defined here

View File

@ -348,6 +348,12 @@ describe Rackstash::Logger do
logger.unknown { 'Unknown' }
expect(messages.last).to include message: 'Unknown', severity: 5
end
it 'can add a raw message with <<' do
logger << :raw_value
expect(messages.last).to include message: :raw_value
expect(messages.last).not_to include :formatter, :severity
end
end
describe '#with_buffer' do

View File

@ -174,10 +174,11 @@ describe Rackstash::Message do
end
it 'accepts non-string objects' do
exception = StandardError.new('An error')
message = Rackstash::Message.new(exception)
message = Rackstash::Message.new(StandardError.new('An error'))
expect(message.to_s).to eql '#<StandardError: An error>'
message = Rackstash::Message.new(:symbol)
expect(message.to_s).to eql ':symbol'
end
it 'is aliased to to_str' do