Class: UmbrellioUtils::SemanticLogger::TinyJsonFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/umbrellio_utils/semantic_logger/tiny_json_formatter.rb

Overview

Simple JSON formatter, represented as callable object.

Examples:

Using of formatter

formatter = UmbrellioUtils::SemanticLogger::TinyJsonFormatter.new
SemanticLogger.add_appender(io: $stdout, formatter: formatter)

Constant Summary collapse

DEFAULT_NAMES_MAPPING =

Hash with default field names in the output JSON.

{
  severity: :severity,
  name: :name,
  thread_fingerprint: :thread_fingerprint,
  message: :message,
  tags: :tags,
  named_tags: :named_tags,
  time: :time,
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(message_size_limit: 10_000, custom_names_mapping: {}) ⇒ UmbrellioUtils::SemanticLogger::TinyJsonFormatter

Returns a new instance of the UmbrellioUtils::SemanticLogger::TinyJsonFormatter.

Examples:

Use custom name for the ‘message` and `time` fields

UmbrellioUtils::SemanticLogger::TinyJsonFormatter.new(
  time: :timestamp, message: :note,
) #=> <UmbrellioUtils::SemanticLogger::TinyJsonFormatter:0x000>

Parameters:

  • [Integer] (Hash)

    a customizable set of options

  • [Hash] (Hash)

    a customizable set of options

  • custom_names_mapping (Hash) (defaults to: {})

    a customizable set of options

Options Hash (custom_names_mapping:):

  • :severity (Symbol)

    custom name for the ‘severity` field.

  • :name (Symbol)

    custom name for the ‘name` field.

  • :thread_fingerprint (Symbol)

    custom name for the thread_fingerprint field.

  • :message (Symbol)

    custom name for the ‘message` field.

  • :tags (Symbol)

    custom name for the ‘tags` field.

  • :named_tags (Symbol)

    custom name for the ‘named_tags` field.

  • :time (Symbol)

    custom name for the ‘time` field.



40
41
42
43
# File 'lib/umbrellio_utils/semantic_logger/tiny_json_formatter.rb', line 40

def initialize(message_size_limit: 10_000, custom_names_mapping: {})
  self.message_size_limit = message_size_limit
  self.field_names = { **DEFAULT_NAMES_MAPPING, **custom_names_mapping }.freeze
end

Instance Method Details

#call(log, _logger) ⇒ String

Formats log structure into the JSON string.

Parameters:

  • log (SemanticLogger::Log)

    log’s data structure.

  • logger (SemanticLogger::Logger)

    active logger.

Returns:

  • (String)

    data



49
50
51
52
# File 'lib/umbrellio_utils/semantic_logger/tiny_json_formatter.rb', line 49

def call(log, _logger)
  data = build_data_for(log)
  data.to_json
end