Class: GrapeLogging::Middleware::RequestLogger
- Inherits:
-
Grape::Middleware::Base
- Object
- Grape::Middleware::Base
- GrapeLogging::Middleware::RequestLogger
- Defined in:
- lib/grape_logging/middleware/request_logger.rb
Instance Attribute Summary collapse
-
#response_body ⇒ Object
Persist response status & response (body) to use int in parameters.
-
#response_status ⇒ Object
Persist response status & response (body) to use int in parameters.
Instance Method Summary collapse
- #after(status, response) ⇒ Object
- #before ⇒ Object
-
#call!(env) ⇒ Object
Call stack and parse responses & status.
-
#initialize(app, options = {}) ⇒ RequestLogger
constructor
A new instance of RequestLogger.
Constructor Details
#initialize(app, options = {}) ⇒ RequestLogger
Returns a new instance of RequestLogger.
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/grape_logging/middleware/request_logger.rb', line 16 def initialize(app, = {}) super @included_loggers = @options[:include] || [] @reporter = if [:instrumentation_key] Reporters::ActiveSupportReporter.new(@options[:instrumentation_key]) else Reporters::LoggerReporter.new(@options[:logger], @options[:formatter], @options[:log_level]) end end |
Instance Attribute Details
#response_body ⇒ Object
Persist response status & response (body) to use int in parameters
14 15 16 |
# File 'lib/grape_logging/middleware/request_logger.rb', line 14 def response_body @response_body end |
#response_status ⇒ Object
Persist response status & response (body) to use int in parameters
14 15 16 |
# File 'lib/grape_logging/middleware/request_logger.rb', line 14 def response_status @response_status end |
Instance Method Details
#after(status, response) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/grape_logging/middleware/request_logger.rb', line 33 def after(status, response) stop_time # Response status @response_status = status @response_body = response # Perform repotters @reporter.perform(collect_parameters) # Invoke loggers invoke_included_loggers(:after) nil end |
#before ⇒ Object
27 28 29 30 31 |
# File 'lib/grape_logging/middleware/request_logger.rb', line 27 def before reset_db_runtime start_time invoke_included_loggers(:before) end |
#call!(env) ⇒ Object
Note:
Exceptions are logged as 500 status & re-raised.
Call stack and parse responses & status.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/grape_logging/middleware/request_logger.rb', line 51 def call!(env) @env = env # Before hook before # Catch error error = catch(:error) do begin @app_response = @app.call(@env) rescue => e # Log as 500 + message after(e.respond_to?(:status) ? e.status : 500, e.) # Re-raise exception raise e end nil end # Get status & response from app_response # when no error occures. if error # Call with error & response after(error[:status], error[:message]) # Throw again throw(:error, error) else status, _, resp = *@app_response # Call after hook properly after(status, resp) end # Otherwise return original response @app_response end |