From 6c2b0a45d9f066083d51c2029be062d261bd99fd Mon Sep 17 00:00:00 2001 From: Peter Solnica Date: Thu, 24 Nov 2022 07:24:47 +0100 Subject: [PATCH] Handle log_if in Proxy constructors --- lib/dry/logger/backends/proxy.rb | 8 ++++++++ lib/dry/logger/dispatcher.rb | 2 +- spec/dry/logger/backends/proxy_spec.rb | 10 ++++++++-- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/dry/logger/backends/proxy.rb b/lib/dry/logger/backends/proxy.rb index c572ab0..8675c1a 100644 --- a/lib/dry/logger/backends/proxy.rb +++ b/lib/dry/logger/backends/proxy.rb @@ -35,6 +35,14 @@ class Proxy < SimpleDelegator end end + # @since 1.0.2 + # @api private + def initialize(backend, **options) + super(backend) + @options = options + self.log_if = @options[:log_if] + end + # @since 1.0.0 # @api private def log?(entry) diff --git a/lib/dry/logger/dispatcher.rb b/lib/dry/logger/dispatcher.rb index 649d460..975681a 100644 --- a/lib/dry/logger/dispatcher.rb +++ b/lib/dry/logger/dispatcher.rb @@ -260,7 +260,7 @@ def add_backend(instance = nil, **backend_options) backend = case (instance ||= Dry::Logger.new(**options, **backend_options)) when Backends::Stream then instance - else Backends::Proxy.new(instance) + else Backends::Proxy.new(instance, **options, **backend_options) end yield(backend) if block_given? diff --git a/spec/dry/logger/backends/proxy_spec.rb b/spec/dry/logger/backends/proxy_spec.rb index db73a71..bb793ee 100644 --- a/spec/dry/logger/backends/proxy_spec.rb +++ b/spec/dry/logger/backends/proxy_spec.rb @@ -87,6 +87,10 @@ def error(exception, **payload) it "supports shortcut log_if" do backend = test_backend do + def info(message) + @stream.write(message) + end + def error(message) @stream.write(message) end @@ -94,8 +98,10 @@ def error(message) logger = Dry.Logger(:test) { |s| s.add_backend(backend, log_if: :error?) } - logger.error("Hello World") + logger.info("Hello World") + logger.error("Oops") - expect(output).to eql("Hello World") + expect(stream).to_not include("Hello World") + expect(stream).to include("Oops") end end