Rails
Send logs and errors from Rails to LogNorth: a Railtie, request middleware, the Rails error subscriber, and Active Job.
bundle add lognorthQuick Start
Section titled “Quick Start”Add credentials:
lognorth: url: https://logs.yoursite.com api_key: your-api-keyThat’s it. The gem auto-configures via Railtie. It sends from every environment except development and test.
Prefer environment variables? Set them in config/application.rb instead of credentials:
config.lognorth.url = ENV["LOGNORTH_URL"]config.lognorth.api_key = ENV["LOGNORTH_API_KEY"]LogNorth.log("User signed up", { user_id: 123 })LogNorth.error("Checkout failed", exception, { order_id: 42 })Middleware
Section titled “Middleware”The middleware automatically:
- Generates a
trace_idper request (or usesX-Trace-IDfrom the incoming header) - Sets
X-Trace-IDon the response - Logs the request with
duration_msandtrace_idas top-level fields - All
LogNorth.log()andLogNorth.error()calls within the request automatically inherit the sametrace_id
class UsersController < ApplicationController def create # This log automatically gets the same trace_id as the middleware request log LogNorth.log("User signed up", { user_id: @user.id }) endendOptions
Section titled “Options”# config/application.rb, with the defaultsconfig.lognorth.enabled = nil # nil: on everywhere except development and testconfig.lognorth.middleware = true # Log HTTP requestsconfig.lognorth.error_subscriber = true # Report exceptions (Rails 7+)config.lognorth.active_job = true # One event per job, with the trace of the request that enqueued itUsers and releases
Section titled “Users and releases”The gem reads Current.user when your app defines it, as the Rails authentication generator does. Otherwise, name the user yourself. Use an ID, never an email:
class ApplicationController < ActionController::Base before_action { LogNorth.user = current_user&.id }endThe issue page then shows how many users an issue hit. Errors also carry the release, from KAMAL_VERSION, LOGNORTH_RELEASE, GIT_SHA, or your host’s commit variable. At boot the gem logs Release <version> started, and LogNorth draws each deploy as a dashed line on its charts.
Ignoring Routes
Section titled “Ignoring Routes”Skip health checks and other noisy endpoints:
config.lognorth.ignored_paths = %w[/healthz /_health /up]These paths won’t be logged by the middleware. The default is ["/up"]; setting the list replaces it.
How It Works
Section titled “How It Works”| Method | Behavior |
|---|---|
log() |
Batched (10 events or 5s) |
error() |
Sent immediately with stack trace |
| Middleware | Logs requests with trace_id + duration_ms |
| Shutdown | Auto-flush on exit |
| ErrorSubscriber | Reports exceptions via Rails.error (7+) |
error() automatically captures the exception class, file, line, caller method, and backtrace. LogNorth groups errors into issues by them.