Class: SiteHub::Middleware::Logging::ErrorLogger

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/sitehub/middleware/logging/error_logger.rb

Constant Summary collapse

LOG_TEMPLATE =
'[%s] ERROR: %s - %s'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, logger = Logger.new(STDERR)) ⇒ ErrorLogger

Returns a new instance of ErrorLogger.



14
15
16
17
# File 'lib/sitehub/middleware/logging/error_logger.rb', line 14

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

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



12
13
14
# File 'lib/sitehub/middleware/logging/error_logger.rb', line 12

def logger
  @logger
end

Instance Method Details

#call(env) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# 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:) ⇒ Object



32
33
34
# 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