Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Logger and IO handler improvements.

* 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...
commit 0fb7017aa3d6cf11e7ccec8a3d8a2dae42e033b7 1 parent 37e6c7b
Hayes Davis authored
8 lib/camayoc/handlers/io.rb
View
@@ -8,15 +8,15 @@ class IO < Logger
include ThreadSafety
- def initialize(io=$stdout,options={})
- super(io,{:method=>:puts}.merge(options))
+ def initialize(io=$stdout,options={},&block)
+ super(io,{:method=>:puts}.merge(options),&block)
self.thread_safe = Camayoc.thread_safe?
end
protected
- def write(type,event)
+ def write(event)
synchronize do
- super(type,event)
+ super(event)
end
end
14 lib/camayoc/handlers/logger.rb
View
@@ -9,14 +9,18 @@ class Logger
attr_accessor :logger, :method, :formatter
- def initialize(logger, options={})
+ def initialize(logger, options={}, &block)
self.logger = logger
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
def event(ev)
- msg = formatter.call(ev.type,ev)
+ msg = formatter.call(ev)
if @method
@logger.send(@method,msg)
else
@@ -25,8 +29,8 @@ def event(ev)
end
def default_formatter
- Proc.new do |type,event|
- "#{type} #{event.ns_stat} #{event.value} #{Time.now.utc.to_i}"
+ Proc.new do |event|
+ "#{event.type} #{event.ns_stat} #{event.value} #{Time.now.utc.to_i}"
end
end
28 test/handlers/io_test.rb
View
@@ -10,30 +10,38 @@ def @io.puts(msg)
end
def test_count_sends_correct_log_message
- expect_message(/count foo:bar:baz 500/)
@handler.event(Camayoc::StatEvent.new(:count,"foo:bar","baz",500))
+ assert_message(/count foo:bar:baz 500/)
end
def test_timing_sends_correct_log_message
- expect_message(/timing foo:bar:time 100/)
@handler.event(Camayoc::StatEvent.new(:timing,"foo:bar","time",100))
+ assert_message(/timing foo:bar:time 100/)
end
def test_formatter_changes_format_of_message
- @handler.formatter = Proc.new{|type,event| "#{type}: #{event.ns_stat}"}
- expect_message(/timing: foo:bar:time/)
+ @handler.formatter = Proc.new{|event| "#{event.type}: #{event.ns_stat}"}
@handler.event(Camayoc::StatEvent.new(:timing,"foo:bar","time",100))
+ assert_message(/timing: foo:bar:time/)
end
- def test_ignore_unknown_event_type
- @handler.expects(:count).never
- @handler.expects(:timing).never
- @handler.event(Camayoc::StatEvent.new(:throwaway,"foo:bar","time",500))
+ def test_formatter_can_be_set_with_constructor_block
+ @handler = Camayoc::Handlers::IO.new(@io) do |event|
+ "#{event.type}: #{event.ns_stat} #{event.value}"
+ 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
private
- def expect_message(*messages)
- assert(@io.zip(messages).all?{ |actual,patt| actual =~ patt })
+ def assert_message(*messages)
+ pairs = messages.zip(@io)
+ assert(pairs.all?{ |actual,patt| actual =~ patt },pairs.join("\n"))
end
end
4 test/handlers/logger_test.rb
View
@@ -24,12 +24,12 @@ def test_method_option_changes_method_called_on_logger
end
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/)
@handler.event(Camayoc::StatEvent.new(:timing,"foo:bar","time",100))
end
- def test_ignore_unknown_event_type
+ def test_logs_unknown_event_type
expect_message(:debug,/throwaway foo:bar:time 500/)
@handler.event(Camayoc::StatEvent.new(:throwaway,"foo:bar","time",500))
end
Please sign in to comment.
Something went wrong with that request. Please try again.