Sha256: 81590ab13f22c68fc6a1648eb8382c35fb9321ccca1fdb2c5267fff3882429d6

Contents?: true

Size: 1.19 KB

Versions: 1

Compression:

Stored size: 1.19 KB

Contents

# frozen_string_literal: true

module MrLogaLoga
  module Formatters
    # == Description
    #
    # A simple key value formatter that extends the standard formatter by rendering additional contextual information.
    #
    # == Format
    #
    # The key-value formatter renders messages into the following format:
    #
    # Log format:
    #
    #   SeverityID, [DateTime #pid] SeverityLabel -- ProgName: message key1=value1 key2=value2
    #
    class KeyValue < Logger::Formatter
      # Render a log message
      #
      # @param severity [String] The message severity
      # @param datetime [DateTime] The message date time
      # @param progname [DateTime] The program name
      # @param message [String] The log message
      # @param context [Hash] The log message context
      #
      # @return [String] the formatted log message
      def call(severity, datetime, progname, message, context = {})
        message = message ? msg2str(message).strip : ''
        message = context.map { |key, value| "#{key}=#{value}" }
                    .prepend(message)
                    .compact
                    .join(' ')
        super(severity, datetime, progname, message)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mr_loga_loga-0.2.0 lib/mr_loga_loga/formatters/key_value.rb