Skip to main content

Recall - Ruby SDK

Complete guide to using Recall for structured logging in Ruby.

Basic Logging

BrainzLab::Recall.info("User signed up")
BrainzLab::Recall.debug("Cache hit", key: "user:123")
BrainzLab::Recall.warn("Rate limit approaching", current: 95)
BrainzLab::Recall.error("Payment failed", error: e.message)
BrainzLab::Recall.fatal("Database connection lost")

Structured Data

Pass any serializable data:
BrainzLab::Recall.info("Order placed",
  order: {
    id: order.id,
    total: order.total,
    items: order.items.count
  },
  user: {
    id: user.id,
    plan: user.plan
  },
  metadata: {
    source: "web",
    version: "v2"
  }
)

Context

Global Context

BrainzLab.set_context(
  deployment: "v2.3.1",
  region: "us-east-1"
)

User Context

BrainzLab.set_user(
  id: user.id,
  email: user.email,
  name: user.name
)

Block-Scoped Context

BrainzLab.with_context(order_id: order.id) do
  BrainzLab::Recall.info("Processing order")
  BrainzLab::Recall.info("Charging card")
  BrainzLab::Recall.info("Order complete")
end

Tags

Tags are indexed for fast searching:
BrainzLab.set_tags(
  feature: "checkout",
  experiment: "new_flow"
)

Custom Logger

Create a logger for a specific component:
class PaymentService
  def initialize
    @logger = BrainzLab::Recall.logger("payments")
  end

  def charge(order)
    @logger.info("Charging order", order_id: order.id)
  end
end

Timing

Log with duration:
BrainzLab::Recall.time("external_api_call") do
  response = ExternalAPI.fetch_data
end
# Logs: external_api_call (245.3ms)

Buffering

Logs are buffered and sent in batches:
BrainzLab.configure do |config|
  config.recall_buffer_size = 50      # Flush after 50 logs
  config.recall_flush_interval = 5    # Or every 5 seconds
end
Force an immediate flush:
BrainzLab::Recall.flush

Filtering

Filter sensitive data:
BrainzLab.configure do |config|
  config.scrub_fields = [:password, :credit_card, /token/i]
end

Disable in Tests

# config/environments/test.rb
BrainzLab.configure do |config|
  config.recall_enabled = false
end

Mock for Testing

RSpec.describe UserService do
  it "logs user creation" do
    expect(BrainzLab::Recall).to receive(:info)
      .with("User created", hash_including(user_id: 123))

    UserService.create(id: 123)
  end
end