Skip to content

Instantly share code, notes, and snippets.

@alexey
Last active December 1, 2019 08:13
Show Gist options
  • Save alexey/eb14add5bbb6d92f3317aed5b8d72d2a to your computer and use it in GitHub Desktop.
Save alexey/eb14add5bbb6d92f3317aed5b8d72d2a to your computer and use it in GitHub Desktop.
colorize console output, for rails logs
#!/usr/bin/env ruby -w
# SETUP:
# Copy this file to your $PATH
# cp colorize.rb ~/colorize (or symlink)
# chmod +x ~/bin/colorize
#
# USAGE:
# tail -f log/development.log | colorize
# rails server | colorize
#
# COLORS:
# https://misc.flogisoft.com/bash/tip_colors_and_formatting
class String
FOREGROUND_COLORS = {
black: 30,
blue: 34,
yellow: 33,
cyan: 36,
green: 32,
magenta: 35,
red: 31,
white: 37
}
BACKGROUND_COLORS = {
black: 40,
blue: 44,
yellow: 43,
cyan: 46,
green: 42,
magenta: 45,
red: 41,
white: 107
}
BACKGROUND_COLORS.each do |color, code|
define_method(color) do |fore_color = nil, prefix = ''|
foreground = fore_color ? "#{FOREGROUND_COLORS[fore_color]};" : ''
"\e[#{foreground}#{code}m#{prefix}#{self.gsub("\n", "")}\033[0m"
end
end
end
STDIN.each do |line|
puts case line
when /(Processing by)/
line.green(:black)
when /(Completed 200 OK)/
line.black(:green, '√ ')
when /(\sRendered\s)/
line.cyan(:black)
when /(Error)/
line.red
when /\[INFO/i
line.yellow
else
line
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment