diff --git a/CHANGELOG.md b/CHANGELOG.md index d507dac5..d734152f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### Breaking changes * port / host / tags / namespace can no longer be set on the instance to allow thread-safety [#87][] by [@grosser][] + * replace global logger with instance option [#90][] by [@grosser][] ## 3.3.0 / 2018.02.04 diff --git a/lib/datadog/statsd.rb b/lib/datadog/statsd.rb index 00bd05ba..e4b40b29 100644 --- a/lib/datadog/statsd.rb +++ b/lib/datadog/statsd.rb @@ -78,10 +78,8 @@ class Statsd # Maximum number of metrics in the buffer before it is flushed attr_reader :max_buffer_size - class << self - # Set to a standard logger instance to enable debug logging. - attr_accessor :logger - end + # Logger + attr_reader :logger # Return the current version of the library. # deprecated, but cannot be removed since uses might use it to check the version against older releases @@ -93,10 +91,12 @@ def self.VERSION # @param [Integer] port your statsd port # @option opts [String] :namespace set a namespace to be prepended to every metric name # @option opts [Array] :tags tags to be added to every metric + # @option opts [Loger] :logger for debugging # @option opts [Integer] :max_buffer_size max messages to buffer def initialize(host = DEFAULT_HOST, port = DEFAULT_PORT, opts = EMPTY_OPTIONS) @host = host || DEFAULT_HOST @port = port || DEFAULT_PORT + @logger = opts[:logger] @socket_path = opts[:socket_path] @socket = connect_to_socket if @socket_path.nil? @@ -461,7 +461,7 @@ def sock end def send_to_socket(message) - self.class.logger.debug { "Statsd: #{message}" } if self.class.logger + @logger.debug { "Statsd: #{message}" } if @logger if @socket_path.nil? sock.send(message, 0) else @@ -485,7 +485,7 @@ def send_to_socket(message) end end - self.class.logger.error { "Statsd: #{boom.class} #{boom}" } if self.class.logger + @logger.error { "Statsd: #{boom.class} #{boom}" } if @logger nil end end diff --git a/spec/statsd_spec.rb b/spec/statsd_spec.rb index 011b18d9..6812d6df 100644 --- a/spec/statsd_spec.rb +++ b/spec/statsd_spec.rb @@ -17,7 +17,6 @@ class Datadog::Statsd before do @statsd = Datadog::Statsd.new('localhost', 1234, namespace: namespace) @statsd.socket = FakeUDPSocket.new - Datadog::Statsd.logger = nil end describe ".VERSION" do @@ -369,10 +368,10 @@ def helper_time_return describe "with logging" do require 'stringio' - before { Datadog::Statsd.logger = Logger.new(@log = StringIO.new) } + before { @statsd.instance_variable_set(:@logger, Logger.new(@log = StringIO.new)) } it "should write to the log in debug" do - Datadog::Statsd.logger.level = Logger::DEBUG + @statsd.logger.level = Logger::DEBUG @statsd.increment('foobar') @@ -380,7 +379,7 @@ def helper_time_return end it "should not write to the log unless debug" do - Datadog::Statsd.logger.level = Logger::INFO + @statsd.logger.level = Logger::INFO @statsd.increment('foobar') @@ -428,7 +427,7 @@ class Datadog::Statsd::SomeClass; end describe "handling socket errors" do before do - Datadog::Statsd.logger = Logger.new(@log = StringIO.new) + @statsd.instance_variable_set(:@logger, Logger.new(@log = StringIO.new)) @statsd.socket.instance_eval { def send(*) raise SocketError end } end @@ -442,14 +441,14 @@ class Datadog::Statsd::SomeClass; end end it "works without a logger" do - Datadog::Statsd.logger = nil + @statsd.instance_variable_set(:@logger, nil) @statsd.increment('foobar') end end describe "handling closed socket" do before do - Datadog::Statsd.logger = Logger.new(@log = StringIO.new) + @statsd.instance_variable_set(:@logger, Logger.new(@log = StringIO.new)) end it "should try once to reconnect" do @@ -500,7 +499,6 @@ def send(*) describe "UDS error handling" do before do @statsd = Datadog::Statsd.new('localhost', 1234, {:socket_path => '/tmp/socket'}) - Datadog::Statsd.logger = Logger.new(@log = StringIO.new) end describe "when socket throws connection reset error" do