Class: Fare::Middleware::Logging

Inherits:
Object
  • Object
show all
Defined in:
lib/fare/middleware/logging.rb

Overview

Use this to log all events and stacktraces:

Usage:

use Fare::Middleware::Logging, logger: Logger.new($stdout)

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Logging

Returns a new instance of Logging.



13
14
15
16
# File 'lib/fare/middleware/logging.rb', line 13

def initialize(app, options = {})
  @app = app
  @logger = options.fetch(:logger)
end

Instance Method Details

#call(env) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/fare/middleware/logging.rb', line 18

def call(env)
  attrs = env.fetch(:event).attributes
  start = Time.now
  begin
    @app.call(env)
  rescue Exception => exception
    duration = Time.now - start
    @logger.warn("Handled event: %s" % {
      event:          attrs,
      result:         "failure",
      duration:       duration,
      error_class:    exception.class.name,
      error_message:  exception.message,
      backtrace:      exception.backtrace.to_a,
    }.to_json)
    raise
  else
    duration = Time.now - start
    @logger.info("Handled event: %s" % {
      event:     attrs,
      result:    "success",
      duration:  duration,
    }.to_json)
  end
end