Class: SemanticLogger::Formatters::Color

Inherits:
Default
  • Object
show all
Defined in:
lib/semantic_logger/formatters/color.rb

Defined Under Namespace

Classes: ColorMap

Constant Summary

Constants inherited from Base

Base::PRECISION, Base::TIME_FORMAT

Instance Attribute Summary collapse

Attributes inherited from Default

#log, #logger

Attributes inherited from Base

#log_application, #log_host, #time_format

Instance Method Summary collapse

Methods inherited from Default

#message, #process_info

Methods inherited from Base

#time

Constructor Details

#initialize(ap: {multiline: false}, color_map: ColorMap.new, time_format: TIME_FORMAT, log_host: false, log_application: false) ⇒ 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 chenging the color for info to green.
SemanticLogger.add_appender(io: $stdout, formatter: :color, color_map: {info: SemanticLogger::AnsiColors::YELLOW})

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


53
54
55
56
57
# File 'lib/semantic_logger/formatters/color.rb', line 53

def initialize(ap: {multiline: false}, color_map: ColorMap.new, time_format: TIME_FORMAT, log_host: false, log_application: false)
  @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)
end

Instance Attribute Details

#colorObject

Returns the value of attribute color.



10
11
12
# File 'lib/semantic_logger/formatters/color.rb', line 10

def color
  @color
end

#color_mapObject

Returns the value of attribute color_map.



10
11
12
# File 'lib/semantic_logger/formatters/color.rb', line 10

def color_map
  @color_map
end

Instance Method Details

#call(log, logger) ⇒ Object



98
99
100
101
# File 'lib/semantic_logger/formatters/color.rb', line 98

def call(log, logger)
  self.color = color_map[log.level]
  super(log, logger)
end

#durationObject



76
77
78
# File 'lib/semantic_logger/formatters/color.rb', line 76

def duration
  "(#{color_map.bold}#{log.duration_human}#{color_map.clear})" if log.duration
end

#exceptionObject



94
95
96
# File 'lib/semantic_logger/formatters/color.rb', line 94

def exception
  "-- Exception: #{color}#{log.exception.class}: #{log.exception.message}#{color_map.clear}\n#{log.backtrace_to_s}" if log.exception
end

#levelObject



59
60
61
# File 'lib/semantic_logger/formatters/color.rb', line 59

def level
  "#{color}#{super}#{color_map.clear}"
end

#nameObject



80
81
82
# File 'lib/semantic_logger/formatters/color.rb', line 80

def name
  "#{color}#{super}#{color_map.clear}"
end

#named_tagsObject

Named Tags



68
69
70
71
72
73
74
# File 'lib/semantic_logger/formatters/color.rb', line 68

def named_tags
  if (named_tags = log.named_tags) && !named_tags.empty?
    list = []
    named_tags.each_pair { |name, value| list << "#{color}#{name}: #{value}#{color_map.clear}" }
    "{#{list.join(', ')}}"
  end
end

#payloadObject



84
85
86
87
88
89
90
91
92
# File 'lib/semantic_logger/formatters/color.rb', line 84

def payload
  return unless log.has_payload?

  if !defined?(AwesomePrint) || !log.payload.respond_to?(:ai)
    super
  else
    "-- #{log.payload.ai(@ai_options)}" rescue super
  end
end

#tagsObject



63
64
65
# File 'lib/semantic_logger/formatters/color.rb', line 63

def tags
  "[#{color}#{log.tags.join("#{color_map.clear}] [#{color}")}#{color_map.clear}]" if log.tags && !log.tags.empty?
end