$ rails server にちゃんと色つけて、controllerでlogger.debug check_object したときに表示する値を目視ですぐに確認したいなと思って以下の設定を書いた。
config/initializers/log_formatter.rb
class ActiveSupport::Logger::SimpleFormatter
# from activesupport/lib/active_support/core_ext/logger.rb
def call(severity, time, progname, msg)
"#{severity_msg_color(severity, msg)} \n"
end
private
def severity_msg_color(severity, msg)
case severity
when "DEBUG"
"\033[0;32;40m[DEBUG]\033[0m \033[32m#{String === msg ? msg : msg.inspect}\033[0m"
when "INFO"
"\033[1;30;40m[INFO]\033[0m \033[30m#{String === msg ? msg : msg.inspect}\033[0m"
when "WARN"
"\033[1;33;40m[WARNING]\033[0m \033[33m#{String === msg ? msg : msg.inspect}\033[0m"
when "ERROR"
"\033[0;31;40m[ERROR]\033[0m \033[0;31m#{String === msg ? msg : msg.inspect}\033[0m"
when "FATAL"
"\033[7;31;40m[FATAL]\033[0m \033[31m#{String === msg ? msg : msg.inspect}\033[0m"
else
"[#{severity}]" # none
end
end
end
ANSI Color code (16colors)
# Text attributes
# 0 All attributes off
# 1 Bold on
# 4 Underscore (on monochrome display adapter only)
# 5 Blink on
# 7 Reverse video on
# 8 Concealed on
# Foreground colors
# 0;30 Black 1;30 Dark Gray
# 0;34 Blue 1;34 Light Blue
# 0;32 Green 1;32 Light Green
# 0;36 Cyan 1;36 Light Cyan
# 0;31 Red 1;31 Light Red
# 0;35 Purple 1;35 Light Purple
# 0;33 Brown 1;33 Yellow
# 0;37 Light Gray 1;37 White
# Background colors
# 40 Black
# 41 Red
# 42 Green
# 43 Yellow
# 44 Blue
# 45 Magenta
# 46 Cyan
# 47 White