Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle :log_if in Proxy constructors #23

Merged
merged 1 commit into from
Nov 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/dry/logger/backends/proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/dry/logger/dispatcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
10 changes: 8 additions & 2 deletions spec/dry/logger/backends/proxy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,21 @@ 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
end

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