Sha256: 0667bfd62b0811af0d1ac859b86bd840f32bae1b9e0c21c410838498548b7f95

Contents?: true

Size: 1.2 KB

Versions: 1

Compression:

Stored size: 1.2 KB

Contents

begin
  require 'freddy'
rescue LoadError => e
  raise unless e.message =~ /freddy/
  exception = e.exception('To use RabbitMQ adapter for logging, please install freddy!')
  exception.set_backtrace(e.backtrace)
  raise exception
end

class Logasm
  module Adapters
    class RabbitmqAdapter
      attr_reader :freddy

      CONFIGURATION_KEYS = [:host, :user, :pass, :port]

      def initialize(level, service_name, arguments = {})
        config = arguments.select { |key, value| CONFIGURATION_KEYS.include?(key) }
        logger = Logger.new(STDOUT)
        @queue = arguments.fetch(:queue, 'logstash-queue')
        @level = level
        @service_name = service_name
        @freddy = Freddy.build(logger, config.merge({recover_from_connection_close: true}))
      end

      def log(level, metadata = {})
        if meets_threshold?(level)
          message = Utils.build_event(metadata, level, @service_name)
          deliver_message message
        end
      end

      private

      def meets_threshold?(level)
        LOG_LEVELS.index(level.to_s) >= @level
      end

      def deliver_message(message)
        @freddy.deliver @queue, message
      rescue Bunny::ConnectionClosedError
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
logasm-0.2.2 lib/logasm/adapters/rabbitmq_adapter.rb