Rails4 WEBrick Color

6321 단어 Rails4
$ 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

Log(iTerm2)





참고 링크


  • Beautiful logging for Ruby on Rails 4 | I have a black belt in geek.
  • #56 The Logger (revised) - RailsCasts
  • bash/zsh로 16색(ANSI 컬러 코드)과 256색 컬러 팔레트 표시
  • 좋은 웹페이지 즐겨찾기