Adding colors to your Ruby scripts and console logs: A Developer's guide

As developers, we often work with command-line interfaces and logs. Adding color to your output can significantly improve readability and make important information stand out. Let's explore how to implement colored text in Ruby scripts and Rails logger.

Simple string class extension

One elegant approach is extending Ruby's String class with color methods. Here's how:

# Simple extension to String class for your smaller scripts
class String
  def red
    "\033[31m#{self}\033[0m";
  end

  def brown
    "\033[33m#{self}\033[0m"
  end

  def yellow
    "\033[93m#{self}\033[0m"
  end

  def green
    "\033[32m#{self}\033[0m";
  end

  def magenta
    "\033[35m#{self}\033[0m";
  end

  def cyan
    "\033[36m#{self}\033[0m";
  end

  def bold
    "\033[1m#{self}\033[0m";
  end

  def underline
    "\033[4m#{self}\033[0m"
  end

  def black
    "\033[30m#{self}\033[0m"
  end

  def blue
    "\033[34m#{self}\033[0m"
  end

  def white
    "\033[37m#{self}\033[0m"
  end

  def default_color
    "\033[39m#{self}\033[0m"
  end

  # Background colors
  def bg_black
    "\033[40m#{self}\033[0m"
  end

  def bg_red
    "\033[41m#{self}\033[0m"
  end

  def bg_green
    "\033[42m#{self}\033[0m"
  end

  def bg_yellow
    "\033[43m#{self}\033[0m"
  end

  def bg_blue
    "\033[44m#{self}\033[0m"
  end

  def bg_magenta
    "\033[45m#{self}\033[0m"
  end

  def bg_cyan
    "\033[46m#{self}\033[0m"
  end

  def bg_white
    "\033[47m#{self}\033[0m"
  end

  def bg_default
    "\033[49m#{self}\033[0m"
  end
end

puts "I'm red".red
puts "I'm yellow".yellow
puts "I'm green".green
puts "I'm bold and green".bold.green
puts "I'm brown".brown
puts "I'm magenta".magenta
puts "I'm cyan".cyan
puts "I'm standard"
puts "I'm bold".bold

puts "Red with bg white and underline".red.bg_white.underline
puts "Blue underline".blue.underline

The magic happens through ANSI escape codes. Each color method wraps the string with specific codes: \033[COLOR_CODE] for starting the color and \033[0m for resetting it.

Colorized logging for Rails

For Rails applications, we can extend the logger with colors:

# config/initializers/colorized_logger.rb

module ColorizedLogger
  COLORS = {
    debug: "\e[0;36m",   # Cyan text
    info: "\e[0;32m",    # Green text
    warn: "\e[1;33m",    # Yellow text
    error: "\e[1;31m",   # Red text
    fatal: "\e[1;31m",   # Red text
    unknown: "\e[0m"     # Terminal default
  }.freeze

  RESET_COLOR = "\e[0m"

  COLORS.each_key do |level|
    define_method(level) do |progname = nil, &block|
      message = progname || (block && block.call)
      super("#{COLORS[level]}#{message}#{RESET_COLOR}")
    end
  end
end
Rails.logger.extend(ColorizedLogger)

Something more powerful

If you want something more powerful, you can always be interested in that gem - https://github.com/fazibear/colorize

Summary

Color enhancement in Ruby scripts and console logs transforms the ordinary terminal output into a visually organized and instantly readable interface. By leveraging ANSI escape codes through simple String class extensions, you can implement a flexible system for colorized text that works flawlessly in both standalone scripts and Rails applications. The lightweight yet powerful implementation supports chainable methods for combining colors, backgrounds, and text styles, making it an invaluable tool for debugging, logging, and creating user-friendly command-line experiences.

Happy colorizing!