From 15d3052c846f1a0f7854a31b734da05e21540469 Mon Sep 17 00:00:00 2001 From: Nikita Shilnikov Date: Thu, 31 Aug 2017 02:11:35 +0300 Subject: [PATCH] Make Deprecations.set_logger! accept logger object --- CHANGELOG.md | 6 ++++++ lib/dry/core/deprecations.rb | 21 +++++++++++++++++---- spec/dry/core/deprecations_spec.rb | 23 +++++++++++++++++++++++ 3 files changed, 46 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3971f92..11b5669 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +# v0.3.2 2017-08-31 + +### Added + +* Accept an existing logger object in `Dry::Core::Deprecations.set_logger!` (flash-gordon) + # v0.3.1 2017-05-27 ### Added diff --git a/lib/dry/core/deprecations.rb b/lib/dry/core/deprecations.rb index e089d5a..c601bc8 100644 --- a/lib/dry/core/deprecations.rb +++ b/lib/dry/core/deprecations.rb @@ -86,11 +86,24 @@ def logger(output = nil) # Sets a custom logger. This is a global setting. # - # @option [IO] output output stream for messages + # @overload set_logger!(output) + # @param [IO] output Stream for messages + # + # @overload set_logger! + # Stream messages to stdout + # + # @overload set_logger!(logger) + # @param [#warn] logger + # + # @api public def set_logger!(output = nil) - @logger = Logger.new(output || $stdout) - @logger.formatter = proc { |_severity, _datetime, _progname, msg| "#{msg}\n" } - @logger + if output.respond_to?(:warn) + @logger = output + else + @logger = Logger.new(output || $stdout).tap do |logger| + logger.formatter = proc { |_, _, _, msg| "#{ msg }\n" } + end + end end def [](tag) diff --git a/spec/dry/core/deprecations_spec.rb b/spec/dry/core/deprecations_spec.rb index 55c827f..7bacf51 100644 --- a/spec/dry/core/deprecations_spec.rb +++ b/spec/dry/core/deprecations_spec.rb @@ -137,4 +137,27 @@ def logging(msg) end end end + + describe '.set_logger!' do + let(:logger) do + Class.new { + attr_reader :messages + + def initialize + @messages = [] + end + + def warn(message) + messages << message + end + }.new + end + + it 'accepts preconfigured logger' do + Dry::Core::Deprecations.set_logger!(logger) + Dry::Core::Deprecations.warn("Don't!") + + expect(logger.messages).to eql(%w([deprecated]\ Don't!)) + end + end end