Class: Ougai::Formatters::Colors::Configuration
- Inherits:
-
Object
- Object
- Ougai::Formatters::Colors::Configuration
- 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
-
.default_configuration ⇒ Hash
list default color configuration.
Instance Method Summary collapse
-
#color(subject_key, text, severity) ⇒ String
Convenience method to color a subject for a given log severity.
-
#get_color_for(subject_key, severity) ⇒ String?
Return the color for a given suject and a given severity.
-
#initialize(configuration = {}) ⇒ Configuration
constructor
Configuration accept following keys: -
:load_default_config
If true, then default configuration values is fetched to fill missing value from the provided configuration.
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
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_configuration ⇒ Hash
‘any’ severity label decided in Ougai::Logging::Severity#to_label
values are copied from Ougai::Formatters::Readable
coloring values
list default color configuration
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
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?
!!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
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 |