Sha256: d619c957de50f98a13ae796e41939d632d7bebb1401655f246650671fde5aeb0

Contents?: true

Size: 945 Bytes

Versions: 1

Compression:

Stored size: 945 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'.freeze

      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 do |log_entry|
              log_message(error: log_entry.message, transaction_id: env[RackHttpHeaderKeys::TRANSACTION_ID])
            end

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

      def log_message(error:, transaction_id:)
        format(LOG_TEMPLATE, Time.now.strftime(TIME_STAMP_FORMAT), transaction_id, error)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sitehub-0.4.3 lib/sitehub/logging/error_logger.rb