Sha256: cf52b63b99d3770bc48562c62fe7eeb4b6bc0c236b60348a0732a92b43d7f724

Contents?: true

Size: 1.72 KB

Versions: 4

Compression:

Stored size: 1.72 KB

Contents

require "fluent-logger"
module ExceptionNotifier
  class FluentLoggerNotifier

    attr_accessor :logger, :template
    class ConfigurationError < Exception;end

    def initialize(options)
      @template = options.delete(:template)
      raise ConfigurationError, "`template` key must be set" unless @template

      logger_settings = options.delete(:logger_settings) || {}
      if options.delete(:test_logger)
        @logger = Fluent::Logger::TestLogger.new
      else
        tag_prefix = options.delete(:tag_prefix)
        @logger = Fluent::Logger::FluentLogger.new(tag_prefix, logger_settings)
      end
    end

    def call(exception, options={})
      arg = Argument.build(template, exception, options)
      logger.post(nil, arg)
    end

    class SetupError < Exception;end

    class Argument
      def initialize(template, exception, options = {})
        @exception = exception
        @options = options
        @template = template
      end

      def build
        expand_object(@template)
      end

      def self.build(template, exception, options = {})
        self.new(template, exception, options).build
      end

      private

      def expand_object(obj)
        case obj
        when Hash
          expand_hash(obj)
        when Array
          expand_array(obj)
        when Proc
          expand_proc(obj)
        else
          obj
        end
      end

      def expand_proc(prok)
        expand_object(prok.call(@exception, @options))
      end

      def expand_array(array)
        array.map {|element| expand_object(element) }
      end

      def expand_hash(hash)
        {}.tap do |result|
          hash.each do |k, v|
            result[k] = expand_object(v)
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
exception_notification-fluent_logger_notifier-0.1.0 lib/exception_notifier/fluent_logger_notifier.rb
exception_notification_fluent_logger_notifier-0.0.3 lib/exception_notifier/fluent_logger_notifier.rb
exception_notification_fluent_logger_notifier-0.0.2 lib/exception_notifier/fluent_logger_notifier.rb
exception_notification_fluent_logger_notifier-0.0.1 lib/exception_notifier/fluent_logger_notifier.rb