class SemanticLogger::Formatters::Color

Attributes

color[RW]
color_map[RW]

Public Class Methods

new(ap: {multiline: false}, color_map: ColorMap.new, **args) click to toggle source

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 Amazing Print option for rendering data.
  These options can also be changed be creating a `~/.aprc` file.
  See: https://github.com/amazing-print/amazing_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
Calls superclass method
# File lib/semantic_logger/formatters/color.rb, line 77
def initialize(ap: {multiline: false}, color_map: ColorMap.new, **args)
  @ai_options = ap
  @color_map  = color_map.is_a?(ColorMap) ? color_map : ColorMap.new(color_map)
  super(**args)
end

Public Instance Methods

call(log, logger) click to toggle source
Calls superclass method
# File lib/semantic_logger/formatters/color.rb, line 129
def call(log, logger)
  self.color = color_map[log.level]
  super(log, logger)
end
duration() click to toggle source
# File lib/semantic_logger/formatters/color.rb, line 101
def duration
  "(#{color_map.bold}#{log.duration_human}#{color_map.clear})" if log.duration
end
exception() click to toggle source
# File lib/semantic_logger/formatters/color.rb, line 123
def exception
  return unless log.exception

  "-- Exception: #{color}#{log.exception.class}: #{log.exception.message}#{color_map.clear}\n#{log.backtrace_to_s}"
end
level() click to toggle source
# File lib/semantic_logger/formatters/color.rb, line 83
def level
  "#{color}#{super}#{color_map.clear}"
end
name() click to toggle source
# File lib/semantic_logger/formatters/color.rb, line 105
def name
  "#{color}#{super}#{color_map.clear}"
end
named_tags() click to toggle source

Named Tags

# File lib/semantic_logger/formatters/color.rb, line 92
def named_tags
  named_tags = log.named_tags
  return if named_tags.nil? || named_tags.empty?

  list = []
  named_tags.each_pair { |name, value| list << "#{color}#{name}: #{value}#{color_map.clear}" }
  "{#{list.join(', ')}}"
end
payload() click to toggle source
Calls superclass method
# File lib/semantic_logger/formatters/color.rb, line 109
def payload
  return unless log.payload?

  if log.payload.respond_to?(:ai)
    begin
      "-- #{log.payload.ai(@ai_options)}"
    rescue StandardError
      super
    end
  else
    super
  end
end
tags() click to toggle source
# File lib/semantic_logger/formatters/color.rb, line 87
def tags
  "[#{color}#{log.tags.join("#{color_map.clear}] [#{color}")}#{color_map.clear}]" if log.tags && !log.tags.empty?
end