Class: Lumberjack::Rack::Context
- Inherits:
-
Object
- Object
- Lumberjack::Rack::Context
- Defined in:
- lib/lumberjack/rack/context.rb
Overview
Rack middleware ensures that a global Lumberjack context exists for the duration of each HTTP request. This middleware creates an isolated logging context that automatically cleans up after the request completes, ensuring that request-specific attributes don’t leak between requests.
The middleware supports dynamic attribute extraction from the Rack environment, allowing automatic tagging of log entries with request-specific information such as request IDs, user agents, IP addresses, or any other data available in the Rack environment.
This is particularly useful in web applications where you want to correlate all log entries within a single request with common identifying information, making it easier to trace request flows and debug issues.
Instance Method Summary collapse
-
#call(env) ⇒ Array
Process a Rack request within a scoped Lumberjack logging context.
-
#initialize(app, env_attributes = nil) ⇒ Context
constructor
Initialize the middleware with the Rack application and optional environment attribute configuration.
Constructor Details
#initialize(app, env_attributes = nil) ⇒ Context
Initialize the middleware with the Rack application and optional environment attribute configuration. The middleware will create a scoped logging context for each request and automatically apply the specified attributes.
58 59 60 61 |
# File 'lib/lumberjack/rack/context.rb', line 58 def initialize(app, env_attributes = nil) @app = app @env_attributes = env_attributes end |
Instance Method Details
#call(env) ⇒ Array
Process a Rack request within a scoped Lumberjack logging context.
67 68 69 70 71 72 |
# File 'lib/lumberjack/rack/context.rb', line 67 def call(env) Lumberjack.ensure_context do apply_attributes(env) if @env_attributes @app.call(env) end end |