class SiteHub::Middleware::Logging::ErrorLogger

Constants

LOG_TEMPLATE

Attributes

logger[R]

Public Class Methods

new(app, logger = Logger.new(STDERR)) click to toggle source
# File lib/sitehub/middleware/logging/error_logger.rb, line 14
def initialize(app, logger = Logger.new(STDERR))
  @app = app
  @logger = LogWrapper.new(logger)
end

Public Instance Methods

call(env) click to toggle source
# File lib/sitehub/middleware/logging/error_logger.rb, line 19
def call(env)
  errors = env[ERRORS] ||= LogStash.new
  @app.call(env).tap do
    unless errors.empty?
      messages = 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
log_message(error:, transaction_id:) click to toggle source
# File lib/sitehub/middleware/logging/error_logger.rb, line 32
def log_message(error:, transaction_id:)
  format(LOG_TEMPLATE, Time.now.strftime(TIME_STAMP_FORMAT), transaction_id, error)
end