class StickyElephant::ElephantLogger

Constants

EVENT_CHANNELS

Attributes

hpfeeds[R]
text[R]

Public Class Methods

new(configuration) click to toggle source
# File lib/sticky_elephant/elephant_logger.rb, line 3
def initialize(configuration)
  @text = Logger.new(configuration.log_path)
  @text.level = configuration.log_level
  @hpfeeds = if configuration.use_hpfeeds?
               new_hpfeeds_connection(configuration)
             else
               null_hpfeeds_connection
             end
end

Public Instance Methods

close() click to toggle source
# File lib/sticky_elephant/elephant_logger.rb, line 35
def close
  text.close
  hpfeeds.close
end
event(type, payload) click to toggle source
# File lib/sticky_elephant/elephant_logger.rb, line 24
def event(type, payload)
  unless EVENT_CHANNELS.keys.include? type
    raise ArgumentError.new("Event type #{type} not in #{EVENT_CHANNELS.keys.join(',')}")
  end
  begin
    hpfeeds.publish(payload, EVENT_CHANNELS.fetch(type))
  rescue => e
    warn("#{e.class} received from hpfeeds: #{e}")
  end
end

Private Instance Methods

new_hpfeeds_connection(configuration) click to toggle source
# File lib/sticky_elephant/elephant_logger.rb, line 44
def new_hpfeeds_connection(configuration)
  begin
    ::HPFeeds::Client.new(
      host:      configuration.hpf_host,
      port:      configuration.hpf_port,
      ident:     configuration.hpf_ident,
      secret:    configuration.hpf_secret,
      reconnect: false
    )
  rescue HPFeeds::Exception => e
    warn("#{e.class} received from hpfeeds: #{e}")
    null_hpfeeds_connection
  end
end
noop(*args) click to toggle source
# File lib/sticky_elephant/elephant_logger.rb, line 61
def noop(*args) ; end
null_hpfeeds_connection() click to toggle source
# File lib/sticky_elephant/elephant_logger.rb, line 59
def null_hpfeeds_connection
  @null_hpfeeds_klass ||= Struct.new('NullHPFeedsConnection') do
    def noop(*args) ; end
    alias_method :close, :noop
    alias_method :publish, :noop
  end
  @null_hpfeeds_connection ||= @null_hpfeeds_klass.new
end