mirror of
https://github.com/meineerde/rackstash.git
synced 2025-12-19 23:11:11 +00:00
Allow to overwrite logger attributes in Logger#initialize
This commit is contained in:
parent
4a62e3d189
commit
5eeaab4afa
@ -37,7 +37,7 @@ module Rackstash
|
|||||||
attr_accessor :formatter
|
attr_accessor :formatter
|
||||||
|
|
||||||
# @return [Integer] a numeric log level. Normally you'd use one of the
|
# @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.
|
# log messages with a severity above the configured level.
|
||||||
attr_reader :level
|
attr_reader :level
|
||||||
|
|
||||||
@ -50,14 +50,24 @@ module Rackstash
|
|||||||
# external log targets like a file, a socket, ...
|
# external log targets like a file, a socket, ...
|
||||||
attr_reader :sink
|
attr_reader :sink
|
||||||
|
|
||||||
def initialize(targets)
|
# @param flows [Array<Flow, Object>, Flow, Adapters::Adapter, Object]
|
||||||
@sink = Sink.new(targets)
|
# an array of {Flow}s or a single {Flow}, respectivly object which can be
|
||||||
|
# used as a {Flow}'s adapter. See {Flow#initialize}.
|
||||||
@level = DEBUG
|
# @param level [Integer] a numeric log level. Normally you'd use one of the
|
||||||
@progname = PROGNAME
|
# {SEVERITIES} constants, i.e., an integer between 0 and 5. We will only
|
||||||
@formatter = Formatter.new
|
# 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
|
@buffer_stack = Concurrent::ThreadLocalVar.new
|
||||||
|
|
||||||
|
@sink = Rackstash::Sink.new(flows)
|
||||||
|
self.level = level
|
||||||
|
self.progname = progname
|
||||||
|
self.formatter = formatter
|
||||||
end
|
end
|
||||||
|
|
||||||
# Add a message to the current buffer without any further formatting. If
|
# Add a message to the current buffer without any further formatting. If
|
||||||
|
|||||||
@ -13,6 +13,31 @@ describe Rackstash::Logger do
|
|||||||
let(:target) { StringIO.new }
|
let(:target) { StringIO.new }
|
||||||
let(:logger) { described_class.new(target) }
|
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
|
describe '#formatter' do
|
||||||
it 'defaults to a Rackstash::Formatter' do
|
it 'defaults to a Rackstash::Formatter' do
|
||||||
expect(logger.formatter).to be_a Rackstash::Formatter
|
expect(logger.formatter).to be_a Rackstash::Formatter
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user