diff --git a/lib/rackstash/logger.rb b/lib/rackstash/logger.rb index 7ce4adf..528c953 100644 --- a/lib/rackstash/logger.rb +++ b/lib/rackstash/logger.rb @@ -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, 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 diff --git a/spec/rackstash/logger_spec.rb b/spec/rackstash/logger_spec.rb index 6df0b29..1c9b2dd 100644 --- a/spec/rackstash/logger_spec.rb +++ b/spec/rackstash/logger_spec.rb @@ -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