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

Allow to overwrite logger attributes in Logger#initialize

This commit is contained in:
Holger Just 2017-07-20 00:47:26 +02:00
parent 4a62e3d189
commit 5eeaab4afa
2 changed files with 43 additions and 8 deletions

View File

@ -37,7 +37,7 @@ module Rackstash
attr_accessor :formatter
# @return [Integer] a numeric log level. Normally you'd use one of the
# `SEVERITIES` constants, i.e., an integer between 0 and 5. We will only
# {SEVERITIES} constants, i.e., an integer between 0 and 5. We will only
# log messages with a severity above the configured level.
attr_reader :level
@ -50,14 +50,24 @@ module Rackstash
# external log targets like a file, a socket, ...
attr_reader :sink
def initialize(targets)
@sink = Sink.new(targets)
@level = DEBUG
@progname = PROGNAME
@formatter = Formatter.new
# @param flows [Array<Flow, Object>, Flow, Adapters::Adapter, Object]
# an array of {Flow}s or a single {Flow}, respectivly object which can be
# used as a {Flow}'s adapter. See {Flow#initialize}.
# @param level [Integer] a numeric log level. Normally you'd use one of the
# {SEVERITIES} constants, i.e., an integer between 0 and 5. We will only
# log messages with a severity above the configured level.
# @param progname [String] the logger's progname, used as the default for
# log messages if none is passed to {#add} and passed to the {#formatter}.
# By default we use {PROGNAME}.
# @param formatter [#call] the log formatter for each individual buffered
# line. See {#formatter} for details.
def initialize(flows, level: DEBUG, progname: PROGNAME, formatter: Formatter.new)
@buffer_stack = Concurrent::ThreadLocalVar.new
@sink = Rackstash::Sink.new(flows)
self.level = level
self.progname = progname
self.formatter = formatter
end
# Add a message to the current buffer without any further formatting. If

View File

@ -13,6 +13,31 @@ describe Rackstash::Logger do
let(:target) { StringIO.new }
let(:logger) { described_class.new(target) }
describe '#initialize' do
it 'requires flows' do
expect(Rackstash::Sink).to receive(:new).with('output.log')
logger = described_class.new('output.log')
end
it 'allows to set #level' do
logger = described_class.new('output.log', level: 'ERROR')
expect(logger.level).to eql 3
logger = described_class.new('output.log', level: 2)
expect(logger.level).to eql 2
end
it 'allows to set #progname' do
logger = described_class.new('output.log', progname: 'myapp')
expect(logger.progname).to eql 'myapp'
end
it 'allows to set #formatter' do
logger = described_class.new('output.log', formatter: ->{})
expect(logger.formatter).to be_a Proc
end
end
describe '#formatter' do
it 'defaults to a Rackstash::Formatter' do
expect(logger.formatter).to be_a Rackstash::Formatter