Sha256: e221b7d7e87ebd2ec2ffd6af92689ade00123abaac642553723f24730df81e6f

Contents?: true

Size: 1.33 KB

Versions: 2

Compression:

Stored size: 1.33 KB

Contents

require "log4r/outputter/outputter"
require "redis"
require "retryable"

require_relative "../formatter/json_formatter"

module Log4r
  module Logstash
    class RedisOutputter < Log4r::Outputter
      def initialize(name, hash = {})
        super(name, hash)

        @index = hash[:index] || "logstash"
        @additional_fields = hash[:additional_fields] || {}
        @data_field_name = hash[:data_field_name] || "data"
        @level_field_name = hash[:level_field_name] || "level"
        @timestamp_field_name = hash[:timestamp_field_name] || "timestamp"

        init_redis(hash[:host], hash[:port])
      end

      private

      def init_redis(host, port)
        host ||= "127.0.0.1"
        port = (port || 6379).to_i
        @redis = Redis.new(host: host, port: port)
      end

      def canonical_log(logevent)
        json = JsonFormatter.format(logevent, @index, @data_field_name,
                                    @level_field_name,
                                    @additional_fields)
        tries = 3
        begin
          Retryable.retryable(tries: tries, sleep: ->(n) { 2**n }) do
            @redis.rpush @index, json
          end
        rescue
          raise "Unable to connect to #{@redis.inspect} after #{tries} tries. \
Please confirm your ability to connect to that redis instance."
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
log4r-logstash-0.1.1 lib/log4r/logstash/outputter/redis_outputter.rb
log4r-logstash-0.1.0 lib/log4r/logstash/outputter/redis_outputter.rb