Sha256: 47d00369289a9fc1185d31ad5f5b1ebc1b89b00060b200cea07bfd6f4959a478

Contents?: true

Size: 1.21 KB

Versions: 2

Compression:

Stored size: 1.21 KB

Contents

# frozen_string_literal: true

module MrLogaLoga
  module Formatters
    # == Description
    #
    # A simple Json formatter for MrLogaLoga.
    #
    # == Format
    #
    # The json formatter renders messages into a single-line json. Context keys are embedded on the top level.
    #
    # Log Format:
    #
    #   { "severity": "Severity", .. "message": "Message", "key1": "Key1" }
    #
    class Json < Logger::Formatter
      # Render a log message in JSON
      #
      # @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.nil? || message.empty? ? nil : msg2str(message)

        message_hash = {
          severity: severity,
          datetime: datetime.strftime('%Y-%m-%dT%H:%M:%S.%6N'),
          pid: Process.pid,
          progname: progname,
          message: message,
          **context
        }.compact
        "#{message_hash.to_json}\n"
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mr_loga_loga-0.1.2 lib/mr_loga_loga/formatters/json.rb
mr_loga_loga-0.1.1 lib/mr_loga_loga/formatters/json.rb