Replacing Rails Logger with Lograge

Among the biggest problems of the default Rails logger are that it's:

  • too verbose
  • not parsable

which makes it a bad fit for production.

Let's add a Lograge to fight those issues:

gem "lograge"

The configuration below is the one I use in my current project, config/initializers/lograge.rb:

# frozen_string_literal: true

Rails.application.configure do
  config.lograge.enabled = true
  config.lograge.keep_original_rails_log = true
  config.lograge.formatter =
  config.lograge.logger =
  config.lograge.ignore_actions = [

Additionaly I silenced the Active Record queries spam, by setting config/environments/production.rb

config.active_record.logger = nil

It works great, but does not allow to use the default Rails.logger.*severity* logger anymore. So I created a custom events logger lib/log.rb:

# frozen_string_literal: true

# Adds logger Log.*level* methods
module Log
  extend self

  %i[debug info warn error fatal unknown].each do |severity|
    define_method severity do |message, params = {}|
      raise ArgumentError, "Hash is expected as 'params'" unless params.is_a?(Hash)
      logger.public_send(severity, {
        m: message


  def logger

Now it's possible to logs events like this: "SMS received", from: from, body: body, message_id: message_id

And get a neat parsable output:

{"m":"SMS received","from":"+XXXXXXXXXX","body":"Your verification code is: 5752","message_id":19932478}

Part 2: Stream Rails Logs to Google Cloud Stackdriver