Sha256: 527377ef817b6cbf3a4cdd03bf53fb123e696ee02d8a33c74fa71b29cbbab383

Contents?: true

Size: 904 Bytes

Versions: 2

Compression:

Stored size: 904 Bytes

Contents

require 'logger'
require 'sitehub/constants'
require_relative 'log_wrapper'
require_relative 'log_stash'
class SiteHub
  module Logging
    class ErrorLogger
      include Constants
      LOG_TEMPLATE = '[%s] ERROR: %s - %s'


      attr_reader :logger

      def initialize(app, logger = Logger.new(STDERR))
        @app = app
        @logger = LogWrapper.new(logger)
      end

      def call env
        env[ERRORS] ||= LogStash.new
        @app.call(env).tap do
          unless env[ERRORS].empty?
            messages = env[ERRORS].collect { |log_entry| log_message(error: log_entry.message, transaction_id: env[RackHttpHeaderKeys::TRANSACTION_ID]) }

            logger.write(messages.join(NEW_LINE))
          end
        end
      end

      def log_message(error:, transaction_id:)
        LOG_TEMPLATE % [Time.now.strftime(TIME_STAMP_FORMAT), transaction_id, error]
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sitehub-0.4.2 lib/sitehub/logging/error_logger.rb
sitehub-0.4.1 lib/sitehub/logging/error_logger.rb