Class: Dcmgr::Rack::RequestLogger
- Inherits:
-
Object
- Object
- Dcmgr::Rack::RequestLogger
- Defined in:
- lib/dcmgr/rack/request_logger.rb
Overview
Rack middleware for logging each API request.
Constant Summary collapse
- HTTP_X_VDC_REQUEST_ID =
'HTTP_X_VDC_REQUEST_ID'.freeze
- HEADER_X_VDC_REQUEST_ID =
'X-VDC-Request-ID'.freeze
Instance Method Summary collapse
- #_call(env) ⇒ Object
- #call(env) ⇒ Object
-
#initialize(app, with_header = true) ⇒ RequestLogger
constructor
A new instance of RequestLogger.
Constructor Details
#initialize(app, with_header = true) ⇒ RequestLogger
Returns a new instance of RequestLogger.
9 10 11 12 13 |
# File 'lib/dcmgr/rack/request_logger.rb', line 9 def initialize(app, with_header=true) raise TypeError unless app.is_a?(Dcmgr::Endpoints::CoreAPI) @app = app @with_header = with_header end |
Instance Method Details
#_call(env) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/dcmgr/rack/request_logger.rb', line 19 def _call(env) @log = Dcmgr::Models::RequestLog.new log_env(env) begin ret = @app.call(env) @log.response_status = ret[0] @log.response_msg = '' # inject X-VDC-Request-ID header if @with_header ret[1] = (ret[1] || {}).merge({HEADER_X_VDC_REQUEST_ID=>@log.request_id}) end return ret rescue ::Exception => e @log.response_status = 999 @log.response_msg = e. raise e ensure @log.class.db.transaction do @log.save end end end |
#call(env) ⇒ Object
15 16 17 |
# File 'lib/dcmgr/rack/request_logger.rb', line 15 def call(env) dup._call(env) end |