Class: LogStruct::SemanticLogger::ColorFormatter

Inherits:
SemanticLogger::Formatters::Color
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/log_struct/semantic_logger/color_formatter.rb

Overview

Writes lines as pretty-printed JSON in color, for reading in a terminal: keys yellow, strings green, numbers blue, booleans magenta, nil red and logger names cyan (config.color_map changes them). LogStruct uses it in development when LogStruct is enabled there, prefer_json_in_development is false and enable_color_output is true (its default). LogStruct entries and Hashes are the JSON line, pretty-printed. Plain lines keep SemanticLogger's layout, with their message, exception and tags scrubbed and their named tags filtered as a JSON line's ctx is.

Instance Method Summary collapse

Constructor Details

#initialize(color_map: nil, **args) ⇒ void

Parameters:

  • color_map (Hash{Symbol => Symbol}, nil) (defaults to: nil)
  • args (T.untyped)


19
20
21
22
23
24
25
26
# File 'lib/log_struct/semantic_logger/color_formatter.rb', line 19

def initialize(color_map: nil, **args)
  super(**args)
  @logstruct_formatter = T.let(LogStruct::Formatter.new, LogStruct::Formatter)
  @json_formatter = T.let(LogStruct::SemanticLogger::Formatter.new, LogStruct::SemanticLogger::Formatter)

  # Set up custom color mapping
  @custom_colors = T.let(default_color_map.merge(color_map || {}), T::Hash[Symbol, Symbol])
end

Instance Method Details

#call(log, logger) ⇒ String

Parameters:

  • log (::SemanticLogger::Log)
  • logger (T.untyped)

Returns:

  • (String)


29
30
31
32
33
34
35
36
37
38
# File 'lib/log_struct/semantic_logger/color_formatter.rb', line 29

def call(log, logger)
  # Handle LogStruct types specially with colorization
  if log.payload.is_a?(Hash) || log.payload.is_a?(T::Struct)
    formatted = format_logstruct_payload(log, logger)
    formatted if formatted
  else
    # For plain messages, use SemanticLogger's default colorization
    super
  end
end

#exception ⇒ T.untyped

SemanticLogger's text for the exception and its causes, without the input a params parser quoted

Returns:

  • (T.untyped)


53
54
55
56
57
58
59
60
# File 'lib/log_struct/semantic_logger/color_formatter.rb', line 53

def exception
  text = super
  error = log.exception
  if text.is_a?(String) && error.is_a?(Exception)
    ParamsErrors.replacements(error).each { |quoted, logged| text = text.gsub(quoted, logged) }
  end
  scrub_text(text)
end

#message ⇒ T.untyped

The parts of a plain line that hold what was logged, scrubbed like the strings in a JSON line

Returns:

  • (T.untyped)


42
43
44
# File 'lib/log_struct/semantic_logger/color_formatter.rb', line 42

def message
  scrub_text(super)
end

#named_tags ⇒ String?

Filtered and scrubbed as they are in a JSON line's ctx: a key filtering hides there is hidden here too

Returns:

  • (String, nil)


69
70
71
72
73
74
75
76
# File 'lib/log_struct/semantic_logger/color_formatter.rb', line 69

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

  filtered = @logstruct_formatter.process_values({ctx: named_tags.to_h.transform_keys(&:to_sym)})[:ctx]
  list = filtered.map { |name, value| "#{color}#{name}: #{value}#{color_map.clear}" }
  "{#{list.join(", ")}}"
end

#payload ⇒ T.untyped

Returns:

  • (T.untyped)


47
48
49
# File 'lib/log_struct/semantic_logger/color_formatter.rb', line 47

def payload
  scrub_text(super)
end

#tags ⇒ T.untyped

Returns:

  • (T.untyped)


63
64
65
# File 'lib/log_struct/semantic_logger/color_formatter.rb', line 63

def tags
  scrub_text(super)
end