Module: ActiveSupport::ColorizeLogging

Extended by:
Concern
Included in:
EventReporter::LogSubscriber, LogSubscriber
Defined in:
activesupport/lib/active_support/colorize_logging.rb

Overview

:nodoc:

Constant Summary collapse

MODES =

ANSI sequence modes

{
  clear:     0,
  bold:      1,
  italic:    3,
  underline: 4,
}
BLACK =

ANSI sequence colors

"\e[30m"
RED =
"\e[31m"
GREEN =
"\e[32m"
YELLOW =
"\e[33m"
BLUE =
"\e[34m"
MAGENTA =
"\e[35m"
CYAN =
"\e[36m"
WHITE =
"\e[37m"

Instance Method Summary collapse

Methods included from Concern

append_features, class_methods, extended, included, prepend_features, prepended

Instance Method Details

#color(text, color, mode_options = {}) ⇒ Object

Set color by using a symbol or one of the defined constants. Set modes by specifying bold, italic, or underline options. Inspired by Highline, this method will automatically clear formatting at the end of the returned String.



52
53
54
55
56
57
58
# File 'activesupport/lib/active_support/colorize_logging.rb', line 52

def color(text, color, mode_options = {}) # :doc:
  return text unless colorize_logging
  color = self.class.const_get(color.upcase) if color.is_a?(Symbol)
  mode = mode_from(mode_options)
  clear = "\e[#{MODES[:clear]}m"
  "#{mode}#{color}#{text}#{clear}"
end

#colorize_loggingObject



66
67
68
# File 'activesupport/lib/active_support/colorize_logging.rb', line 66

def colorize_logging
  ActiveSupport.colorize_logging
end

#debug(progname = nil, &block) ⇒ Object



29
30
31
# File 'activesupport/lib/active_support/colorize_logging.rb', line 29

def debug(progname = nil, &block)
  logger.debug(progname, &block) if logger
end

#error(progname = nil, &block) ⇒ Object



37
38
39
# File 'activesupport/lib/active_support/colorize_logging.rb', line 37

def error(progname = nil, &block)
  logger.error(progname, &block) if logger
end

#fatal(progname = nil, &block) ⇒ Object



41
42
43
# File 'activesupport/lib/active_support/colorize_logging.rb', line 41

def fatal(progname = nil, &block)
  logger.fatal(progname, &block) if logger
end

#info(progname = nil, &block) ⇒ Object



25
26
27
# File 'activesupport/lib/active_support/colorize_logging.rb', line 25

def info(progname = nil, &block)
  logger.info(progname, &block) if logger
end

#mode_from(options) ⇒ Object



60
61
62
63
64
# File 'activesupport/lib/active_support/colorize_logging.rb', line 60

def mode_from(options)
  modes = MODES.values_at(*options.compact_blank.keys)

  "\e[#{modes.join(";")}m" if modes.any?
end

#unknown(progname = nil, &block) ⇒ Object



45
46
47
# File 'activesupport/lib/active_support/colorize_logging.rb', line 45

def unknown(progname = nil, &block)
  logger.unknown(progname, &block) if logger
end

#warn(progname = nil, &block) ⇒ Object



33
34
35
# File 'activesupport/lib/active_support/colorize_logging.rb', line 33

def warn(progname = nil, &block)
  logger.warn(progname, &block) if logger
end