Taming Ruby on Rails Logging with Lograge and Mezmo

4 MIN READ
MIN READ

LogDNA is now Mezmo but the product you know and live is here to stay

Ruby on Rails is a powerful and intuitive framework for building web applications. With its low barrier to entry, getting started is fast—but as applications grow in complexity, maintaining clear, actionable logs becomes essential for full stack monitoring.

Rails was originally designed when applications lived on a single server. Every interaction—loading a page, submitting a form—generates multiple verbose log entries, often across several lines, making it difficult to track a single user’s request or pinpoint failures.

Here’s a typical log sample for one simple action:

Started GET "/" for 127.0.0.1 at 2021-07-10 09:42:10 +0700 Processing by HomeController#index as HTML Rendered text template within layouts/application (0.0ms) Rendered layouts/_assets.html.erb (2.0ms) Rendered layouts/_top.html.erb (2.6ms) Rendered layouts/_about.html.erb (0.3ms) Completed 200 OK in 79ms (Views: 78.8ms | ActiveRecord: 0.0ms)

While manageable for small applications, this log fragmentation becomes problematic at scale, especially in containerized and serverless environments. Add in distributed systems, and parsing these scattered lines manually becomes nearly impossible.

That’s where Lograge enters the picture, bringing structure and clarity for teams managing complex telemetry pipelines.

In response to Rails' noisy logging, Mathias Meyer developed Lograge—a tool built to streamline Rails logs into structured, machine-parseable, single-line outputs. The result? Logs that are compact and highly usable in any telemetry pipeline.

Here’s what the example log looks like after applying Lograge:

{"method":"GET", "format:"html", "path":"/ controller=HomeController", "action":"home#index", "status":200, "duration":79, "view":78.8, "db":7.4}

This structure helps platforms like Mezmo ingest logs cleanly for APM logs, visualization, and correlation with traces and metrics, enabling deep application insight.

With structured logs enriched like this, Mezmo users benefit from enhanced traceability and the ability to feed detailed APM logs into dashboards and alerts.

# config/environments/development.rb Rails.application.configure do config.lograge.enabled = true config.lograge.custom_options = lambda do |event| { application: Rails.application.class.parent_name, host: event.payload[:host], rails_env: Rails.env, process_id: Process.pid, request_id: event.payload[:headers]['action_dispatch.request_id'], request_time: Time.now, remote_ip: event.payload[:remote_ip], ip: event.payload[:ip], x_forwarded_for: event.payload[:x_forwarded_for], params: event.payload[:params].except(*exceptions).to_json, exception: event.payload[:exception]&.first, exception_message: "#{event.payload[:exception]&.last}", exception_backtrace: event.payload[:exception_object]&.backtrace&.join(",") }.compact end end

To get the additional custom variables in the event payload you must add them to your controller. For instance, we could do something like

# app/controllers/application_controller.rb class ApplicationController < ActionController::Base def append_info_to_payload(payload) super payload[:host] = request.host paylod[:x_forwarded_for] = request.env['HTTP_X_FORWARDED_FOR'] end end

Since Meyer designed Lograge to integrate naturally with Ruby's built-in logging, we can use Mezmo's own Ruby/Rails logger to ingest logs from your application. In two short steps, you can have your entire application sending logs to Mezmo.

Step One: Include the logdna gem

gem 'logdna'

Step Two: Configure the Rails logger to use Mezmo with JSON-formatted Lograge

Rails.application.configure do config.lograge.enabled = true config.lograge.formatter = Lograge::Formatters::Json.new config.logger = Logdna::Ruby.new(YOUR_API_KEY, OPTIONS) end

And with that, your logs are both concise and aggregated, allowing you to monitor and troubleshoot your Rails app faster.

Pro Tip

Mezmo displays the message field as the human-readable headline of any log entry, making it easy to scan logs in the viewer. All other fields remain fully searchable and usable for filtering, alerting, and dashboarding.

Want to see this in action? Check out our GitHub guide to building a Rails decoder app with Lograge + Mezmo or contact one of our logging experts.

Join the Mezmo community, explore our docs, and level up your full stack monitoring today.

Table of Contents

    Share Article

    RSS Feed