Logging Rails requests with structure and colors

In a related post about Flask and Python I explained how to structure request logs in Python with a sprinkle of colors.

Rails already has the great lograge but how can we leverage it and add ANSI colors to the strings?

Fortunately lograge supports custom formatters with:

Rails.application.configure do
  config.lograge.enabled = true
  config.lograge.formatter = YourOwnFormatter.new
end

so I just created a new formatter to add colors like this:

require 'colorized_string'

class ColorKeyValue < Lograge::Formatters::KeyValue
  FIELDS_COLORS = {
    method: :red,
    path: :red,
    format: :red,
    controller: :green,
    action: :green,
    status: :yellow,
    duration: :magenta,
    view: :magenta,
    db: :magenta,
    time: :cyan,
    ip: :red,
    host: :red,
    params: :green
  }

  def format(key, value)
    line = super(key, value)

    color = FIELDS_COLORS[key] || :default
    ColorizedString.new(line).public_send(color)
  end
end

I admit that color coding each parameter might be a little too much but I’m having fun :-D

require 'colorized_string' and ColorizedString are part of the colorize library.

This is the result:

colorized logging