Class: Core::ApiLogger
- Defined in:
- lib/svcbase/middleware/apilogger.rb
Overview
cribbed heavily from grape-middleware-logger
Constant Summary collapse
- BACKSLASH =
'/'
Class Attribute Summary collapse
-
.filter ⇒ Object
Returns the value of attribute filter.
-
.logger ⇒ Object
Returns the value of attribute logger.
Instance Attribute Summary collapse
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Instance Method Summary collapse
- #call!(env) ⇒ Object
-
#initialize(_unused, options = {}) ⇒ ApiLogger
constructor
A new instance of ApiLogger.
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, = {}) super @options[:filter] ||= self.class.filter @logger = [:logger] || self.class.logger || self.class.default_logger end |
Class Attribute Details
.filter ⇒ Object
Returns the value of attribute filter.
18 19 20 |
# File 'lib/svcbase/middleware/apilogger.rb', line 18 def filter @filter end |
.logger ⇒ Object
Returns the value of attribute logger.
18 19 20 |
# File 'lib/svcbase/middleware/apilogger.rb', line 18 def logger @logger end |
Instance Attribute Details
#logger ⇒ Object (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 |