Class: Camayoc::Handlers::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/camayoc/handlers/logger.rb

Overview

Write stats to a logger. Specify the method to call on the logger instance with :method (usually something like :info). If not :method is specified :debug will be called on the logger. You can control the format of the message passed to the logger method using the :formatter Proc.

Direct Known Subclasses

IO

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger, options = {}, &block) ⇒ Logger

Returns a new instance of Logger.



12
13
14
15
16
17
18
19
20
# File 'lib/camayoc/handlers/logger.rb', line 12

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

Instance Attribute Details

#formatterObject

Returns the value of attribute formatter.



10
11
12
# File 'lib/camayoc/handlers/logger.rb', line 10

def formatter
  @formatter
end

#loggerObject

Returns the value of attribute logger.



10
11
12
# File 'lib/camayoc/handlers/logger.rb', line 10

def logger
  @logger
end

#methodObject

Returns the value of attribute method.



10
11
12
# File 'lib/camayoc/handlers/logger.rb', line 10

def method
  @method
end

Instance Method Details

#default_formatterObject



31
32
33
34
35
# File 'lib/camayoc/handlers/logger.rb', line 31

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

#event(ev) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/camayoc/handlers/logger.rb', line 22

def event(ev)
  msg = formatter.call(ev)
  if @method
    @logger.send(@method,msg)
  else
    @logger.debug(msg)
  end
end