Logging with Recall
Recall provides a simple API for structured logging that integrates seamlessly with Rails.
Basic Usage
BrainzLab::Recall.info("User signed up", user_id: user.id)
Each log entry includes:
- level - The log level (debug, info, warn, error, fatal)
- message - A human-readable message
- data - Structured data as key-value pairs
- timestamp - When the log was created
- context - Automatic context (request ID, user, etc.)
Log Levels
Debug
Use for detailed debugging information:
BrainzLab::Recall.debug("Cache lookup",
key: "user:#{user.id}",
hit: true,
ttl_remaining: 3600
)
Info
General informational messages about application flow:
BrainzLab::Recall.info("Order placed",
order_id: order.id,
total: order.total,
payment_method: "card"
)
Warn
Warning conditions that should be reviewed:
BrainzLab::Recall.warn("Rate limit approaching",
user_id: user.id,
current: 95,
limit: 100
)
Error
Error conditions that need attention:
BrainzLab::Recall.error("Payment failed",
order_id: order.id,
error: "Card declined",
amount: 99.99
)
Fatal
Critical errors:
BrainzLab::Recall.fatal("Database connection lost",
host: "db.example.com",
error: e.message
)
Structured Data
Pass any serializable data as keyword arguments:
BrainzLab::Recall.info("Complex event",
user: { id: user.id, email: user.email },
order: { id: order.id, items: order.items.count },
metadata: { source: "api", version: "v2" }
)
Avoid logging sensitive data like passwords, API keys, or credit card numbers.
Best Practices
Be Descriptive
# Good
BrainzLab::Recall.info("User subscription upgraded",
user_id: user.id,
from_plan: "free",
to_plan: "pro"
)
# Avoid
BrainzLab::Recall.info("upgrade", user_id: user.id)
Include Context
# Good - includes all relevant context
BrainzLab::Recall.error("Payment failed",
user_id: user.id,
order_id: order.id,
amount: order.total,
error_code: "card_declined",
error_message: e.message
)
# Avoid - missing context
BrainzLab::Recall.error("Payment failed")
Use Appropriate Levels
# Use debug for verbose output
BrainzLab::Recall.debug("SQL query executed", sql: query, duration_ms: 15)
# Use info for business events
BrainzLab::Recall.info("User completed onboarding", user_id: user.id)
# Use error for actual errors
BrainzLab::Recall.error("Failed to send email", error: e.message)
Rails Logger Integration
You can replace the Rails logger with Recall:
# config/initializers/brainzlab.rb
Rails.logger = BrainzLab::Recall::Logger.new
This captures all Rails.logger calls and sends them to Recall.
Filtering Sensitive Data
Configure data scrubbing:
BrainzLab.configure do |config|
config.recall_scrub_fields = [
:password,
:credit_card,
:ssn,
/secret/i
]
end