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::CONTROL_CHARS, Base::CONTROL_CHAR_ESCAPES, Base::PRECISION, Base::UNICODE_CONTROL_CHARS

Instance Attribute Summary collapse

Attributes inherited from Base

#backtrace_cleaner, #escape_control_chars, #log, #log_application, #log_environment, #log_exception_details, #log_host, #logger, #precision, #time_format

Instance Method Summary collapse

Methods inherited from Default

#exception_details, #file_name_and_line, #message, #method_name, #process_info, #thread_name

Methods inherited from Base

build_time_format, #pid, #time

Constructor Details

#initialize(ap: {multiline: false}, color_map: ColorMap.new, **args) ⇒ 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 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.
Note: With `escape_control_chars: true` the payload is always rendered plain
     (without colors) and on a single line, then escaped.

color_map: [Hash | SemanticLogger::Formatters::Color::ColorMap] ColorMaps each of the log levels to a color



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

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

Instance Attribute Details

#color ⇒ Object

Returns the value of attribute color.



15
16
17
# File 'lib/semantic_logger/formatters/color.rb', line 15

def color
  @color
end

#color_map ⇒ Object

Returns the value of attribute color_map.



15
16
17
# File 'lib/semantic_logger/formatters/color.rb', line 15

def color_map
  @color_map
end

Instance Method Details

#call(log, logger) ⇒ Object



138
139
140
141
# File 'lib/semantic_logger/formatters/color.rb', line 138

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

#duration ⇒ Object



108
109
110
# File 'lib/semantic_logger/formatters/color.rb', line 108

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

#exception ⇒ Object



130
131
132
133
134
135
136
# File 'lib/semantic_logger/formatters/color.rb', line 130

def exception
  return unless log.exception

  name    = Utils.exception_name(log.exception)
  message = escape_control_characters(Utils.exception_message(log.exception))
  "-- Exception: #{color}#{name}: #{message}#{color_map.clear}#{exception_detail_lines}"
end

#level ⇒ Object



85
86
87
# File 'lib/semantic_logger/formatters/color.rb', line 85

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

#name ⇒ Object



112
113
114
# File 'lib/semantic_logger/formatters/color.rb', line 112

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

#named_tags ⇒ Object

Named Tags



97
98
99
100
101
102
103
104
105
106
# File 'lib/semantic_logger/formatters/color.rb', line 97

def named_tags
  named_tags = log.named_tags
  return if named_tags.nil? || named_tags.empty?

  list = []
  named_tags.each_pair do |name, value|
    list << "#{color}#{escape_control_characters(name)}: #{escape_control_characters(value)}#{color_map.clear}"
  end
  "{#{list.join(', ')}}"
end

#payload ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/semantic_logger/formatters/color.rb', line 116

def payload
  return unless log.payload?

  if log.payload.respond_to?(:ai)
    begin
      "-- #{escape_control_characters(log.payload_to_s { |payload| payload.ai(ai_options) })}"
    rescue StandardError
      super
    end
  else
    super
  end
end

#tags ⇒ Object



89
90
91
92
93
94
# File 'lib/semantic_logger/formatters/color.rb', line 89

def tags
  return if log.tags.nil? || log.tags.empty?

  tags = log.tags.map { |tag| escape_control_characters(tag) }
  "[#{color}#{tags.join("#{color_map.clear}] [#{color}")}#{color_map.clear}]"
end