6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Rails4 WEBrick Color

Posted at

$ 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)

スクリーンショット 2015-03-23 13.09.58.png

参考リンク

6
6
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?