Class: Lumberjack::Rack::Context

Inherits:
Object
  • Object
show all
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.

Examples:

Basic usage in a Rack application

use Lumberjack::Rack::Context

With static attributes

use Lumberjack::Rack::Context, {
  app_name: "MyWebApp",
  version: "1.2.3"
}

With dynamic attributes from request environment

use Lumberjack::Rack::Context, {
  request_id: ->(env) { env["HTTP_X_REQUEST_ID"] },
  user_agent: ->(env) { env["HTTP_USER_AGENT"] },
  remote_ip: ->(env) { env["REMOTE_ADDR"] },
  method: ->(env) { env["REQUEST_METHOD"] },
  path: ->(env) { env["PATH_INFO"] }
}

Rails integration

# In config/application.rb
config.middleware.use Lumberjack::Rack::Context, {
  request_id: ->(env) { env["action_dispatch.request_id"] },
  session_id: ->(env) { env["rack.session"]&.id },
  user_id: ->(env) { env["warden"]&.user&.id }
}

See Also:

Instance Method Summary collapse

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.

Parameters:

  • app (Object)

    The next Rack application in the middleware stack

  • env_attributes (Hash, nil) (defaults to: nil)

    Optional hash defining attributes to extract from the request environment. Values can be:

    • Static values: Applied directly to all requests

    • Proc objects: Called with the Rack env hash to generate dynamic values

    • Any callable: Invoked with env to produce request-specific 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.

Parameters:

  • env (Hash)

    The Rack environment hash containing request information

Returns:

  • (Array)

    The standard Rack response array [status, headers, body]



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