DEV Community

rhymes
rhymes

Posted on • Updated on • Originally published at rhymes.dev

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


Enter fullscreen mode Exit fullscreen mode

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


Enter fullscreen mode Exit fullscreen mode

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

Top comments (9)

Collapse
 
srichsun profile image
EvanW

can you describe more about where to put ColorKeyValue class ? I would be very appreicated
I'm trying to make YourOwnFormatter.new work but failed.

I created a file called color_key_value.rb and put those code inside
...
require 'colorized_string'
class ColorKeyValue < Lograge::Formatters::KeyValue
...
and
config.lograge.formatter = ColorKeyValue.new in development.rb

but it shows uninitialized constant ColorKeyValue (NameError)

Collapse
 
rhymes profile image
rhymes

You should put everything in a lograge initializer.

# config/initializers/lograge.rb
require 'colorized_string'

class ColorKeyValue < Lograge::Formatters::KeyValue
  # ...
end

Rails.application.configure do
  config.lograge.enabled = true
  config.lograge.formatter = ColorKeyValue.new
end
Collapse
 
alvinwch profile image
AlvinWCH

thank you ! but I think got the error at this line : color = FIELDS_COLORS[key] || :default

Could not log "process_action.action_controller" event. NoMethodError: undefined method `default' for "user_type=NONE":ColorizedString ["/Users/alvin/Documents/flw/config/initializers/lograge.rb:24

Thread Thread
 
rhymes profile image
rhymes

Can you share the whole file? You can use stuff gist.github.com/ or pastebin.com/

Collapse
 
philnash profile image
Phil Nash

That's really cool and I love how simple it is. Thanks for sharing!

Collapse
 
andy profile image
Andy Zhao (he/him)

Colorize is great 👍

Collapse
 
richjdsmith profile image
Rich Smith

Awesome! Love how simple this is.

Collapse
 
tidelake profile image
Jesse Crockett

Can we add background color to the rails log, using this gem. Thank you so much for this!

Collapse
 
rhymes profile image
rhymes

I think so, the colorize gem used in the example supports them: github.com/fazibear/colorize#usage