class Logit::Context

Overview

Manages contextual data that is automatically included in log events.

Context provides a way to attach additional metadata to log events without passing it through method arguments. There are two types of context:

  1. Fiber context - Persists for the lifetime of the fiber. Use this for request-scoped data like request IDs, user IDs, or session information.

  2. Method context - Cleared after each instrumented method completes. Use this for temporary data relevant only to the current operation.

Fiber Context (Request-Scoped)

Fiber context persists across all method calls within the same fiber:

# In your request handler
Logit.add_fiber_context(request_id: "abc-123", user_id: "user-456")

# All subsequent log events in this fiber will include these values
process_request(data)  # logged with request_id and user_id
save_to_database(data) # also logged with request_id and user_id

# Clear when the request completes
Logit.clear_fiber_context

Method Context (Operation-Scoped)

Method context is automatically cleared after each instrumented method:

class OrderService
  @[Logit::Log]
  def process_order(order_id : Int32) : Bool
    # Add context for just this operation
    Logit.add_context(step: "validation")
    validate_order(order_id)

    Logit.add_context(step: "payment")
    charge_payment(order_id)

    true
  end  # context is cleared here
end

Scoped Context

Use .with_fiber_context to temporarily set context for a block:

Logit::Context.with_fiber_context(transaction_id: "txn-789") do
  # All logs in this block include transaction_id
  process_transaction
end  # transaction_id is automatically removed

Context Priority

When both fiber and method context contain the same key, method context takes precedence.

Defined in:

logit/context.cr

Class Method Summary

Class Method Detail

def self.clear_fiber : Nil #

Clears all fiber-local context.


[View source]
def self.clear_method : Nil #

Clears all method-local context.


[View source]
def self.current : Hash(String, String) #

Returns the merged context (fiber + method, method takes precedence).


[View source]
def self.delete(key : String) : String | Nil #

Deletes a method-local context value.


[View source]
def self.get_fiber(key : String) : String | Nil #

Gets a fiber-local context value.


[View source]
def self.get_method(key : String) : String | Nil #

Gets a method-local context value.


[View source]
def self.set_fiber(**kwargs) : Nil #

Sets fiber-local context values that persist across method calls.


[View source]
def self.set_fiber_hash(hash : Hash(String, String)) : Nil #

Sets fiber-local context from a hash.


[View source]
def self.set_fiber_named_tuple(named_tuple : NamedTuple) : Nil #

Sets fiber-local context from a named tuple.


[View source]
def self.set_method(**kwargs) : Nil #

Sets method-local context values (cleared after method completes).


[View source]
def self.set_method_hash(hash : Hash(String, String)) : Nil #

Sets method-local context from a hash.


[View source]
def self.set_method_named_tuple(named_tuple : NamedTuple) : Nil #

Sets method-local context from a named tuple.


[View source]
def self.with_fiber_context(**kwargs, &) #

Executes a block with temporary fiber context.

The provided context values are added for the duration of the block, then restored to their previous state afterward.

Logit::Context.with_fiber_context(request_id: "abc-123") do
  # request_id is available here
  process_request
end
# request_id is no longer set

[View source]