diff --git a/lib/rackstash.rb b/lib/rackstash.rb index 6001537..e1db00d 100644 --- a/lib/rackstash.rb +++ b/lib/rackstash.rb @@ -89,3 +89,4 @@ require 'rackstash/logger' require 'rackstash/adapters/callable' require 'rackstash/adapters/file' require 'rackstash/adapters/io' +require 'rackstash/adapters/null' diff --git a/lib/rackstash/adapters/null.rb b/lib/rackstash/adapters/null.rb new file mode 100644 index 0000000..be77af8 --- /dev/null +++ b/lib/rackstash/adapters/null.rb @@ -0,0 +1,43 @@ +# frozen_string_literal: true + +# Copyright 2017 Holger Just +# +# This software may be modified and distributed under the terms +# of the MIT license. See the LICENSE.txt file for details. + +require 'rackstash/adapters/adapter' +require 'rackstash/encoders/raw' + +module Rackstash + module Adapters + # This adapter swallows all logs sent to it without writing them anywhere. + # + # It is probably not very useful for production use but can be used to test + # the {Flow} pipeline. + class Null < Adapter + register_for NilClass + + # Create a new black hole adapter. Any logs written to it will be + # swallowed and not written anywhere. + def initialize(*) + end + + # By default, we use a {Rackstash::Encoders::Raw} encoder to encode the + # events. Since we are ignoreing them anyway, there is no need for fancy + # formatting here. + # + # @return [Rackstash::Encoders::Raw] a new Raw encoder + def default_encoder + Rackstash::Encoders::Raw.new + end + + # Swallow a log event. It is not written anywhere. + # + # @param log [Object] the encoded log event + # @return [nil] + def write_single(log) + nil + end + end + end +end diff --git a/spec/rackstash/adapters/null_spec.rb b/spec/rackstash/adapters/null_spec.rb new file mode 100644 index 0000000..e165cde --- /dev/null +++ b/spec/rackstash/adapters/null_spec.rb @@ -0,0 +1,45 @@ +# frozen_string_literal: true + +# Copyright 2017 Holger Just +# +# This software may be modified and distributed under the terms +# of the MIT license. See the LICENSE.txt file for details. + +require 'spec_helper' + +require 'rackstash/adapters/null' + +describe Rackstash::Adapters::Null do + let(:adapter) { described_class.new } + + describe '#initialize' do + it 'accepts and ignores any arguments' do + expect { described_class.new }.not_to raise_error + expect { described_class.new(:foo, :bar, :baz) }.not_to raise_error + end + end + + describe '.default_encoder' do + it 'returns a Raw encoder' do + expect(adapter.default_encoder).to be_instance_of Rackstash::Encoders::Raw + end + end + + describe '#close' do + it 'does nothing' do + expect { adapter.close }.not_to raise_error + end + end + + describe '#reopen' do + it 'does nothing' do + expect { adapter.reopen }.not_to raise_error + end + end + + describe '#write_single' do + it 'does nothing' do + expect { adapter.write('a log line') }.not_to raise_error + end + end +end