Skip to content

Rails

Add logging to Rails apps

# Gemfile
gem "lognorth", github: "karloscodes/lognorth-sdk-rails"

Add credentials:

config/credentials.yml.enc
lognorth:
url: https://logs.yoursite.com
api_key: your-api-key

That’s it. The gem auto-configures via Railtie.

config/initializers/lognorth.rb
LogNorth.config(
ENV["LOGNORTH_URL"],
ENV["LOGNORTH_API_KEY"]
)
LogNorth.log("User signed up", { user_id: 123 })
LogNorth.error("Checkout failed", exception, { order_id: 42 })

The middleware automatically:

  • Generates a trace_id per request (or uses X-Trace-ID from the incoming header)
  • Sets X-Trace-ID on the response
  • Logs the request with duration_ms and trace_id as top-level fields
  • All LogNorth.log() and LogNorth.error() calls within the request automatically inherit the same trace_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 })
end
end
config/application.rb
config.lognorth.enabled = Rails.env.production?
config.lognorth.middleware = true # Log HTTP requests
config.lognorth.error_subscriber = true # Report exceptions (Rails 7+)

Skip health checks and other noisy endpoints:

config/application.rb
config.lognorth.ignored_paths = %w[/healthz /_health /up]

These paths won’t be logged by the middleware.

MethodBehavior
log()Batched (10 events or 5s)
error()Sent immediately with stack trace
MiddlewareLogs requests with trace_id + duration_ms
ShutdownAuto-flush on exit
ErrorSubscriberReports exceptions via Rails.error (7+)

error() automatically captures the exception class, file, line, caller method, and backtrace. These are used for three-tier issue grouping.