Sha256: 400212b518182eb9777768dbdc13e6e974a7c2e7654afac66d0b35219e76ddf9

Contents?: true

Size: 1.25 KB

Versions: 2

Compression:

Stored size: 1.25 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 [Object] The log message, which may not be a string
      # @param context [Hash] The log message context
      #
      # @return [String] the formatted log message
      def call(severity, datetime, progname, message, **context)
        message = message.nil? ? '' : msg2str(message).strip

        message_hash = {
          severity: severity,
          datetime: datetime.strftime('%Y-%m-%dT%H:%M:%S.%6N'),
          pid: Process.pid,
          progname: progname,
          message: (message.empty? ? nil : 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.5 lib/mr_loga_loga/formatters/json.rb
mr_loga_loga-0.1.4 lib/mr_loga_loga/formatters/json.rb