Class: Core::ApiLogger

Inherits:
Grape::Middleware::Globals
  • Object
show all
Defined in:
lib/svcbase/middleware/apilogger.rb

Overview

cribbed heavily from grape-middleware-logger

Constant Summary collapse

BACKSLASH =
'/'

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_unused, options = {}) ⇒ ApiLogger

Returns a new instance of ApiLogger.



21
22
23
24
25
# File 'lib/svcbase/middleware/apilogger.rb', line 21

def initialize(_unused, options = {})
  super
  @options[:filter] ||= self.class.filter
  @logger = options[:logger] || self.class.logger || self.class.default_logger
end

Class Attribute Details

.filterObject

Returns the value of attribute filter.



18
19
20
# File 'lib/svcbase/middleware/apilogger.rb', line 18

def filter
  @filter
end

.loggerObject

Returns the value of attribute logger.



18
19
20
# File 'lib/svcbase/middleware/apilogger.rb', line 18

def logger
  @logger
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



15
16
17
# File 'lib/svcbase/middleware/apilogger.rb', line 15

def logger
  @logger
end

Instance Method Details

#call!(env) ⇒ Object

Note:

Error and exception handling are required for the after hooks Exceptions are logged as a 500 status and re-raised Other “errors” are caught, logged and re-thrown



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/svcbase/middleware/apilogger.rb', line 30

def call!(env)
  @env = env
  before
  error = catch(:error) do
    app_response = @app.call(@env)
    @status, = *app_response
    return app_response # NB: this exits the entire function, not just this block
  end
  # this is reached only if we caught an error! throw
  @status = handle_throw(error)
  # convert grape 401/500 to App 401/500 but skip logging (uncommon)
  raise Core::Exceptions::Unauthorized, loglevel: :none if @status == 401
  raise Core::Exceptions::Fatal, loglevel: :none if @status == 500
  throw(:error, error)
rescue StandardError => e
  @status = handle_exception(e)
  raise
ensure
  # this will run regardless of whether we exit via raise, throw, or return
  after(@status)
end