Class: CxLog::Middleware
- Inherits:
-
Object
- Object
- CxLog::Middleware
- Defined in:
- lib/cx_log/middleware.rb
Overview
Rails middleware to emit a contextual log line for each request
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, **kwargs) ⇒ Middleware
constructor
A new instance of Middleware.
Constructor Details
#initialize(app, **kwargs) ⇒ Middleware
Returns a new instance of Middleware.
6 7 8 9 |
# File 'lib/cx_log/middleware.rb', line 6 def initialize(app, **kwargs) @app = app CxLog::Log.instance. = kwargs end |
Instance Method Details
#call(env) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/cx_log/middleware.rb', line 11 def call(env) CxLog::Log.add(request_id: env["action_dispatch.request_id"]) method = env["REQUEST_METHOD"] route = Rails.application.routes.recognize_path(env["PATH_INFO"]) CxLog::Log.instance.add( controller: route[:controller], action: route[:action], method: method ) @app.call(env) rescue StandardError => e CxLog::Log.instance.add(error: e.) raise e ensure CxLog::Log.instance.flush(Rails.logger) end |