Sha256: acbe70896d1d650d4e90f00937ec2b536dcc594445a233bc19fa411d1a86731d

Contents?: true

Size: 1.74 KB

Versions: 25

Compression:

Stored size: 1.74 KB

Contents

module Services
  module Logger
    class Redis
      META_CLASSES = [
        NilClass,
        TrueClass,
        FalseClass,
        Symbol,
        String,
        Numeric
      ].freeze

      InvalidMetaError                 = Class.new(StandardError)
      EmptyResponseFromRedisMultiError = Class.new(StandardError)

      def initialize(redis, key = 'logs')
        @redis, @key = redis, key
      end

      def log(message, meta = {}, severity = 'info')
        # Allow only simple data types in meta
        raise InvalidMetaError, "Meta keys and values must be of one of the following classes: #{META_CLASSES.join(', ')}" if meta_includes_invalid_values?(meta)

        value = {
          time:     Time.now.to_i,
          message:  message.to_s,
          severity: severity.to_s,
          meta:     meta
        }
        @redis.lpush @key, value.to_json
      end

      def size
        @redis.llen @key
      end

      def fetch
        @redis.lrange(@key, 0, -1).map(&method(:log_entry_from_json))
      end

      def clear
        response = 3.tries on: EmptyResponseFromRedisMultiError do
          @redis.multi do
            @redis.lrange @key, 0, -1
            @redis.del @key
          end or raise EmptyResponseFromRedisMultiError
        end
        response.first.map(&method(:log_entry_from_json))
      end

      private

      def log_entry_from_json(json)
        data = JSON.load(json)
        data['time'] = Time.at(data['time'])
        data
      end

      def meta_includes_invalid_values?(meta)
        [meta.values, meta.keys].any? do |elements|
          elements.any? do |element|
            META_CLASSES.none? do |klass|
              element.class <= klass
            end
          end
        end
      end
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
services-9.0.0 lib/services/logger/redis.rb
services-8.0.1 lib/services/logger/redis.rb
services-8.0.0 lib/services/logger/redis.rb
services-7.3.3 lib/services/logger/redis.rb
services-7.3.1 lib/services/logger/redis.rb
services-7.3.0 lib/services/logger/redis.rb
services-7.2.1 lib/services/logger/redis.rb
services-7.2.0 lib/services/logger/redis.rb
services-7.1.2 lib/services/logger/redis.rb
services-7.1.1 lib/services/logger/redis.rb
services-7.1.0 lib/services/logger/redis.rb
services-7.0.3 lib/services/logger/redis.rb
services-7.0.2 lib/services/logger/redis.rb
services-7.0.1 lib/services/logger/redis.rb
services-7.0.0 lib/services/logger/redis.rb
services-6.0.5 lib/services/logger/redis.rb
services-6.0.4 lib/services/logger/redis.rb
services-6.0.3 lib/services/logger/redis.rb
services-6.0.2 lib/services/logger/redis.rb
services-6.0.1 lib/services/logger/redis.rb