Class: SemanticLogger::Formatters::Color
- Defined in:
- lib/semantic_logger/formatters/color.rb
Defined Under Namespace
Classes: ColorMap
Constant Summary
Constants inherited from Base
Instance Attribute Summary collapse
-
#color ⇒ Object
Returns the value of attribute color.
-
#color_map ⇒ Object
Returns the value of attribute color_map.
Attributes inherited from Default
Attributes inherited from Base
#log_application, #log_host, #precision, #time_format
Instance Method Summary collapse
- #call(log, logger) ⇒ Object
- #duration ⇒ Object
- #exception ⇒ Object
-
#initialize(ap: {multiline: false}, color_map: ColorMap.new, time_format: nil, log_host: false, log_application: false, precision: PRECISION) ⇒ Color
constructor
Adds color to the default log formatter.
- #level ⇒ Object
- #name ⇒ Object
-
#named_tags ⇒ Object
Named Tags.
- #payload ⇒ Object
- #tags ⇒ Object
Methods inherited from Default
Methods inherited from Base
Constructor Details
#initialize(ap: {multiline: false}, color_map: ColorMap.new, time_format: nil, log_host: false, log_application: false, precision: PRECISION) ⇒ Color
Adds color to the default log formatter
Example:
# Use a colorized output logger.
SemanticLogger.add_appender(io: $stdout, formatter: :color)
Example:
# Use a colorized output logger changing the color for info to yellow.
SemanticLogger.add_appender(io: $stdout, formatter: {color: {color_map: {info: SemanticLogger::AnsiColors::YELLOW}}})
Example:
# Override the Awesome Print options to output hashes over multiple lines:
SemanticLogger.add_appender(io: $stdout, formatter: {color: {ap: {multiline: true}}})
# Calling the appender added above:
SemanticLogger['Test'].info('hi', {a: 1, b: 2})
=> true
=> 2019-02-12 11:47:50.794339 I [35832:70112015269920] Test -- hi -- {
:a => 1,
:b => 2
}
Parameters:
ap: [Hash]
Any valid AwesomePrint option for rendering data.
These options can also be changed be creating a `~/.aprc` file.
See: https://github.com/michaeldv/awesome_print
Note: The option :multiline is set to false if not supplied.
Note: Has no effect if Awesome Print is not installed.
color_map: [Hash | SemanticLogger::Formatters::Color::ColorMap]
ColorMaps each of the log levels to a color
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/semantic_logger/formatters/color.rb', line 73 def initialize(ap: {multiline: false}, color_map: ColorMap.new, time_format: nil, log_host: false, log_application: false, precision: PRECISION) @ai_options = ap @color_map = color_map.is_a?(ColorMap) ? color_map : ColorMap.new(color_map) super(time_format: time_format, log_host: log_host, log_application: log_application, precision: precision) end |
Instance Attribute Details
#color ⇒ Object
Returns the value of attribute color.
11 12 13 |
# File 'lib/semantic_logger/formatters/color.rb', line 11 def color @color end |
#color_map ⇒ Object
Returns the value of attribute color_map.
11 12 13 |
# File 'lib/semantic_logger/formatters/color.rb', line 11 def color_map @color_map end |
Instance Method Details
#call(log, logger) ⇒ Object
130 131 132 133 |
# File 'lib/semantic_logger/formatters/color.rb', line 130 def call(log, logger) self.color = color_map[log.level] super(log, logger) end |
#duration ⇒ Object
102 103 104 |
# File 'lib/semantic_logger/formatters/color.rb', line 102 def duration "(#{color_map.bold}#{log.duration_human}#{color_map.clear})" if log.duration end |
#exception ⇒ Object
124 125 126 127 128 |
# File 'lib/semantic_logger/formatters/color.rb', line 124 def exception return unless log.exception "-- Exception: #{color}#{log.exception.class}: #{log.exception.}#{color_map.clear}\n#{log.backtrace_to_s}" end |
#level ⇒ Object
84 85 86 |
# File 'lib/semantic_logger/formatters/color.rb', line 84 def level "#{color}#{super}#{color_map.clear}" end |
#name ⇒ Object
106 107 108 |
# File 'lib/semantic_logger/formatters/color.rb', line 106 def name "#{color}#{super}#{color_map.clear}" end |
#named_tags ⇒ Object
Named Tags
93 94 95 96 97 98 99 100 |
# File 'lib/semantic_logger/formatters/color.rb', line 93 def = log. return if .nil? || .empty? list = [] .each_pair { |name, value| list << "#{color}#{name}: #{value}#{color_map.clear}" } "{#{list.join(', ')}}" end |
#payload ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/semantic_logger/formatters/color.rb', line 110 def payload return unless log.payload? if !defined?(AwesomePrint) || !log.payload.respond_to?(:ai) super else begin "-- #{log.payload.ai(@ai_options)}" rescue StandardError super end end end |
#tags ⇒ Object
88 89 90 |
# File 'lib/semantic_logger/formatters/color.rb', line 88 def "[#{color}#{log..join("#{color_map.clear}] [#{color}")}#{color_map.clear}]" if log. && !log..empty? end |