Navigation Menu

Skip to content

Commit

Permalink
Logger and IO handler improvements.
Browse files Browse the repository at this point in the history
* Formatters now take an event argument instead of type and event
* Accept a constructor block that will be treated as a formatter
* Fix lots of issues with the IO unit test
  • Loading branch information
Hayes Davis committed Nov 17, 2011
1 parent 37e6c7b commit 0fb7017
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 21 deletions.
8 changes: 4 additions & 4 deletions lib/camayoc/handlers/io.rb
Expand Up @@ -8,15 +8,15 @@ class IO < Logger


include ThreadSafety include ThreadSafety


def initialize(io=$stdout,options={}) def initialize(io=$stdout,options={},&block)
super(io,{:method=>:puts}.merge(options)) super(io,{:method=>:puts}.merge(options),&block)
self.thread_safe = Camayoc.thread_safe? self.thread_safe = Camayoc.thread_safe?
end end


protected protected
def write(type,event) def write(event)
synchronize do synchronize do
super(type,event) super(event)
end end
end end


Expand Down
14 changes: 9 additions & 5 deletions lib/camayoc/handlers/logger.rb
Expand Up @@ -9,14 +9,18 @@ class Logger


attr_accessor :logger, :method, :formatter attr_accessor :logger, :method, :formatter


def initialize(logger, options={}) def initialize(logger, options={}, &block)
self.logger = logger self.logger = logger
self.method = options[:method] self.method = options[:method]
self.formatter = (options[:formatter] || default_formatter) if block_given?
self.formatter = block
else
self.formatter = (options[:formatter] || default_formatter)
end
end end


def event(ev) def event(ev)
msg = formatter.call(ev.type,ev) msg = formatter.call(ev)
if @method if @method
@logger.send(@method,msg) @logger.send(@method,msg)
else else
Expand All @@ -25,8 +29,8 @@ def event(ev)
end end


def default_formatter def default_formatter
Proc.new do |type,event| Proc.new do |event|
"#{type} #{event.ns_stat} #{event.value} #{Time.now.utc.to_i}" "#{event.type} #{event.ns_stat} #{event.value} #{Time.now.utc.to_i}"
end end
end end


Expand Down
28 changes: 18 additions & 10 deletions test/handlers/io_test.rb
Expand Up @@ -10,30 +10,38 @@ def @io.puts(msg)
end end


def test_count_sends_correct_log_message def test_count_sends_correct_log_message
expect_message(/count foo:bar:baz 500/)
@handler.event(Camayoc::StatEvent.new(:count,"foo:bar","baz",500)) @handler.event(Camayoc::StatEvent.new(:count,"foo:bar","baz",500))
assert_message(/count foo:bar:baz 500/)
end end


def test_timing_sends_correct_log_message def test_timing_sends_correct_log_message
expect_message(/timing foo:bar:time 100/)
@handler.event(Camayoc::StatEvent.new(:timing,"foo:bar","time",100)) @handler.event(Camayoc::StatEvent.new(:timing,"foo:bar","time",100))
assert_message(/timing foo:bar:time 100/)
end end


def test_formatter_changes_format_of_message def test_formatter_changes_format_of_message
@handler.formatter = Proc.new{|type,event| "#{type}: #{event.ns_stat}"} @handler.formatter = Proc.new{|event| "#{event.type}: #{event.ns_stat}"}
expect_message(/timing: foo:bar:time/)
@handler.event(Camayoc::StatEvent.new(:timing,"foo:bar","time",100)) @handler.event(Camayoc::StatEvent.new(:timing,"foo:bar","time",100))
assert_message(/timing: foo:bar:time/)
end end


def test_ignore_unknown_event_type def test_formatter_can_be_set_with_constructor_block
@handler.expects(:count).never @handler = Camayoc::Handlers::IO.new(@io) do |event|
@handler.expects(:timing).never "#{event.type}: #{event.ns_stat} #{event.value}"
@handler.event(Camayoc::StatEvent.new(:throwaway,"foo:bar","time",500)) end
@handler.event(Camayoc::StatEvent.new(:timing,"foo:bar","time",100))
assert_message(/timing: foo:bar:time 100/)
end

def test_logs_any_event_type
@handler.event(Camayoc::StatEvent.new(:baz,"foo:bar","time",500))
assert_message(/baz foo:bar:time 500/)
end end


private private
def expect_message(*messages) def assert_message(*messages)
assert(@io.zip(messages).all?{ |actual,patt| actual =~ patt }) pairs = messages.zip(@io)
assert(pairs.all?{ |actual,patt| actual =~ patt },pairs.join("\n"))
end end


end end
4 changes: 2 additions & 2 deletions test/handlers/logger_test.rb
Expand Up @@ -24,12 +24,12 @@ def test_method_option_changes_method_called_on_logger
end end


def test_formatter_changes_format_of_message def test_formatter_changes_format_of_message
@handler.formatter = Proc.new{|type,event| "#{type}: #{event.ns_stat}"} @handler.formatter = Proc.new{|event| "#{event.type}: #{event.ns_stat}"}
expect_message(:debug,/timing: foo:bar:time/) expect_message(:debug,/timing: foo:bar:time/)
@handler.event(Camayoc::StatEvent.new(:timing,"foo:bar","time",100)) @handler.event(Camayoc::StatEvent.new(:timing,"foo:bar","time",100))
end end


def test_ignore_unknown_event_type def test_logs_unknown_event_type
expect_message(:debug,/throwaway foo:bar:time 500/) expect_message(:debug,/throwaway foo:bar:time 500/)
@handler.event(Camayoc::StatEvent.new(:throwaway,"foo:bar","time",500)) @handler.event(Camayoc::StatEvent.new(:throwaway,"foo:bar","time",500))
end end
Expand Down

0 comments on commit 0fb7017

Please sign in to comment.