Class: Ougai::Formatters::Colors::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/ougai/formatters/colors/configuration.rb

Overview

Handle the colorization of output, mainly aimed at console formatting. The configuration,split by subject such as level, msg, or datetime is basically a Hash: config with the subject as key and values. Values can be have three types:

  • String: the color escape sequence for the subject

  • Hash: the color escape sequence per severity. If not all severities are defined, a :default value must be defined

  • Symbol: refers to another key and same coloring is applied

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration = {}) ⇒ Configuration

Configuration accept following keys:

  • :load_default_config If true, then default configuration values is fetched to fill missing value from the provided configuration. Default is true.

  • any remaining key is considered to be color-related and is translated into a *subject => color rule* mapping

Parameters:

  • configuration (Hash) (defaults to: {})

    color configuration. Cannot be nil



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/ougai/formatters/colors/configuration.rb', line 48

def initialize(configuration = {})
  # check if loading or not from default configuration
  if configuration.fetch(:load_default_config) { true }
    @config = Configuration.default_configuration
  else
    @config = {}
  end

  configuration.each do |key, val|
    default_val = @config[key]
    # default value is a Hash AND input value is a Hash => merge
    if val.is_a?(Hash) && default_val.is_a?(Hash)
      @config[key] = default_val.merge(val)
    # Input value is assigned because one of the follow
    # 1) input value is not defined in default configuration
    # 2) input value is not a Hash which overrides the default value
    # 3) default value is not a Hash and input is a Hash => Arbitrary
    else
      @config[key] = val
    end
  end
end

Class Method Details

.default_configurationHash

Note:

‘any’ severity label decided in Ougai::Logging::Severity#to_label

Note:

values are copied from Ougai::Formatters::Readable coloring values

list default color configuration

Returns:

  • (Hash)

    default color configuration is severities only



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ougai/formatters/colors/configuration.rb', line 25

def default_configuration
  {
    severity: {
      trace:  Ougai::Formatters::Colors::BLUE,
      debug:  Ougai::Formatters::Colors::WHITE,
      info:   Ougai::Formatters::Colors::CYAN,
      warn:   Ougai::Formatters::Colors::YELLOW,
      error:  Ougai::Formatters::Colors::RED,
      fatal:  Ougai::Formatters::Colors::PURPLE,
      any:    Ougai::Formatters::Colors::GREEN
    }
  }
end

Instance Method Details

#color(subject_key, text, severity) ⇒ String

Convenience method to color a subject for a given log severity

Parameters:

  • subject_key (Symbol)

    to fetch the color to color the text

  • text (String)

    to be colored text

  • severity (Symbol)

    log level

Returns:

  • (String)

    a colored text depending on the subject



78
79
80
81
# File 'lib/ougai/formatters/colors/configuration.rb', line 78

def color(subject_key, text, severity)
  color = get_color_for(subject_key, severity)
  Ougai::Formatters::Colors.color_text(color, text)
end

#get_color_for(subject_key, severity) ⇒ String?

Note:

!!WARNING!!: Circular references are not checked and lead to infinite

Return the color for a given suject and a given severity. This color can then be applied to any text via Ougai::Formatters::Colors.color_text

get_color_for handles color inheritance: if a subject inherit color from another subject, subject value is the symbol refering to the other subject.

loop

Parameters:

  • subject_key (Symbol)

    to define the color to color the text

  • severity (Symbol)

    log level

Returns:

  • (String, nil)

    requested color String value or nil if not colored



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/ougai/formatters/colors/configuration.rb', line 98

def get_color_for(subject_key, severity)
  # no colorization
  return nil unless @config.key?(subject_key)

  # no severity dependence nor inheritance
  color = @config[subject_key]
  return color if color.is_a? String

  # inheritance from another subject
  return get_color_for(color, severity) if color.is_a? Symbol

  # severity dependent but not inherited value or return +nil+ if
  # configuration is incorrect
  severity = severity.downcase.to_sym
  color.fetch(severity) { color[:default] }
end