Class: ShellHelpers::ColorFormatter

Inherits:
Logger::Formatter
  • Object
show all
Defined in:
lib/shell_helpers/logger.rb

Overview

{{{1

Constant Summary collapse

CLI_COLORS =
{
  mark: [:bold],
  success: [:green, :bold],
  important: [:blue, :bold],
  warn: [:yellow, :bold],
  error: [:red, :bold],
  fatal: [:red, :bold]
}
BLANK_FORMAT =
"%{msg}\n"
DEFAULT_FORMAT =

"%s, [%s#%d] %5s -- %s: %s\n"

"%{severity_short}, [%{date}#%<pid>d] %<severity>s9 -- %{progname}: %{msg}\n"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cli_colorsObject

Returns the value of attribute cli_colors.



41
42
43
# File 'lib/shell_helpers/logger.rb', line 41

def cli_colors
  @cli_colors
end

#formatObject



47
48
49
# File 'lib/shell_helpers/logger.rb', line 47

def format
  @format ||= DEFAULT_FORMAT
end

Class Method Details

.create(type = :default) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/shell_helpers/logger.rb', line 17

def self.create(type=:default)
  return type if type.respond_to?(:call)
  logger=self.new
  case type
  when :blank
    logger.format=BLANK_FORMAT
  when :color
    logger.cli_colors=CLI_COLORS
    logger.format=BLANK_FORMAT
  when :color_info
    logger.cli_colors=CLI_COLORS
  when :none
    logger.format=""
  end
  logger
end

Instance Method Details

#call(severity, time, progname, msg, **kwds) ⇒ Object



67
68
69
70
71
72
73
74
75
76
# File 'lib/shell_helpers/logger.rb', line 67

def call(severity, time, progname, msg, **kwds)
  colors=get_colors(severity, **kwds)
  severity_short, severity_name=format_severity(severity)
  format_msg( {severity_short: severity_short,
    date: format_datetime(time),
    pid: $$,
    severity: severity_name,
    progname: progname,
    msg: msg2str(msg)}, colors: colors)
end

#format_msg(msg_infos, colors: []) ⇒ Object



62
63
64
65
# File 'lib/shell_helpers/logger.rb', line 62

def format_msg(msg_infos, colors: [])
  msg_infos[:msg]=SimpleColor[msg_infos[:msg], *colors]
  format % msg_infos
end

#format_severity(severity) ⇒ Object



34
35
36
37
38
39
# File 'lib/shell_helpers/logger.rb', line 34

def format_severity(severity)
  # sev_name = Logger::SEV_LABEL[severity] || 'ANY'
  sev_name=severity.to_s.upcase
  sev_short=sev_name[0..0]
  [sev_name, sev_short]
end