Skip to content

Commit

Permalink
Make Deprecations.set_logger! accept logger object
Browse files Browse the repository at this point in the history
  • Loading branch information
flash-gordon committed Aug 30, 2017
1 parent 4eaf564 commit 15d3052
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
21 changes: 17 additions & 4 deletions lib/dry/core/deprecations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
23 changes: 23 additions & 0 deletions spec/dry/core/deprecations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 15d3052

Please sign in to comment.