Sha256: 229ab2b375282b755f6efea1013cf4f4a2b743a1d7d2eb3b950c94aa83972143

Contents?: true

Size: 1.45 KB

Versions: 2

Compression:

Stored size: 1.45 KB

Contents

module Rack::ActionLogger
  class Emitter

    def initialize
      @can_emit = !Container.get_is_emit_started
      unless @can_emit
        Rack::ActionLogger.logger.error("#{self.class} is already defined.")
        Rack::ActionLogger.logger.error("#{Thread.current.backtrace.join("\n")}")
      end
      @emit_adapter = Rack::ActionLogger.configuration.emit_adapter
      @container = Container
    end

    def emit(context=nil)
      @container.set_is_emit_started
      @container.import(context) if context
      result = yield
      emit_all_logs  # emit log unless exception raised
      result
    ensure
      @container.clear if @can_emit
    end

    private

    def emit_all_logs
      return unless @can_emit
      emit_request_log
      emit_append_logs
    end

    def emit_request_log
      return unless (@container.get_request_log.is_a?(Hash) && @container.get_request_log != {})
      hash = @container.get_request_log.merge @container.get_attributes
      hash = format_tag(hash)
      @emit_adapter.emit(hash)
    end

    def emit_append_logs
      @container.get_append_logs.each do |hash|
        hash = format_tag(hash)
        @emit_adapter.emit(@container.get_attributes.merge(hash))
      end
    end

    def format_tag(hash)
      if hash[:tag]
        hash[:tag] = [Rack::ActionLogger.configuration.tag_prefix, hash[:tag]].join('.')
      else
        hash[:tag] = Rack::ActionLogger.configuration.default_tag
      end
      hash
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rack-action_logger-0.1.9 lib/rack/action_logger/emitter.rb
rack-action_logger-0.1.8 lib/rack/action_logger/emitter.rb