Class: Securial::Middleware::RequestTagLogger
- Inherits:
-
Object
- Object
- Securial::Middleware::RequestTagLogger
- Defined in:
- lib/securial/middleware/request_tag_logger.rb
Overview
Rack middleware that adds request context tags to log messages.
This middleware intercepts requests and wraps the application call with tagged logging, ensuring all log messages generated during request processing include relevant request metadata for better traceability and debugging.
Instance Method Summary collapse
-
#call(env) ⇒ Array
Processes the request with tagged logging context.
-
#initialize(app, logger = Securial.logger) ⇒ RequestTagLogger
constructor
Initializes the middleware with the Rack application and logger.
-
#ip_from_env(env) ⇒ String?
private
private
Extracts the client IP address from the Rack environment.
-
#request_id_from_env(env) ⇒ String?
private
private
Extracts the request ID from the Rack environment.
-
#user_agent_from_env(env) ⇒ String?
private
private
Extracts the user agent string from the Rack environment.
Constructor Details
#initialize(app, logger = Securial.logger) ⇒ RequestTagLogger
Initializes the middleware with the Rack application and logger.
39 40 41 42 |
# File 'lib/securial/middleware/request_tag_logger.rb', line 39 def initialize(app, logger = Securial.logger) @app = app @logger = logger end |
Instance Method Details
#call(env) ⇒ Array
Processes the request with tagged logging context.
Extracts request metadata from the Rack environment and applies them as tags to all log messages generated during the request processing. Tags are automatically removed after the request completes.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/securial/middleware/request_tag_logger.rb', line 60 def call(env) request_id = request_id_from_env(env) ip_address = ip_from_env(env) user_agent = user_agent_from_env(env) = [] << request_id if request_id << "IP:#{ip_address}" if ip_address << "UA:#{user_agent}" if user_agent @logger.tagged(*) do @app.call(env) end end |
#ip_from_env(env) ⇒ String? (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Extracts the client IP address from the Rack environment.
Prioritizes ActionDispatch’s processed remote IP (which handles proxy headers) over the raw REMOTE_ADDR to ensure accurate client identification behind load balancers and proxies.
101 102 103 |
# File 'lib/securial/middleware/request_tag_logger.rb', line 101 def ip_from_env(env) env["action_dispatch.remote_ip"]&.to_s || env["REMOTE_ADDR"] end |
#request_id_from_env(env) ⇒ String? (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Extracts the request ID from the Rack environment.
Looks for request ID in ActionDispatch’s request_id or the X-Request-ID header, providing request traceability across multiple services and log aggregation systems.
87 88 89 |
# File 'lib/securial/middleware/request_tag_logger.rb', line 87 def request_id_from_env(env) env["action_dispatch.request_id"] || env["HTTP_X_REQUEST_ID"] end |
#user_agent_from_env(env) ⇒ String? (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Extracts the user agent string from the Rack environment.
Retrieves the HTTP User-Agent header to provide context about the client application or browser making the request.
114 115 116 |
# File 'lib/securial/middleware/request_tag_logger.rb', line 114 def user_agent_from_env(env) env["HTTP_USER_AGENT"] end |