Sha256: d4a746cf0b0719ed2bd8a3110169a43ea71c67a557bdbc3eb2be83b1d764f1b0

Contents?: true

Size: 1.05 KB

Versions: 4

Compression:

Stored size: 1.05 KB

Contents

# frozen_string_literal: true

require "json"

require "dry/logger/constants"
require "dry/logger/formatters/structured"

module Dry
  module Logger
    module Formatters
      # JSON formatter.
      #
      # This formatter returns log entries in JSON format.
      #
      # @since 0.1.0
      # @api public
      class JSON < Structured
        # @since 0.1.0
        # @api private
        def format(entry)
          hash = format_values(entry).compact
          hash.update(hash.delete(:exception)) if entry.exception?
          ::JSON.dump(hash)
        end

        # @since 0.1.0
        # @api private
        def format_severity(value)
          value.upcase
        end

        # @since 0.1.0
        # @api private
        def format_exception(value)
          {
            exception: value.class,
            message: value.message,
            backtrace: value.backtrace || EMPTY_ARRAY
          }
        end

        # @since 0.1.0
        # @api private
        def format_time(value)
          value.getutc.iso8601
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
dry-logger-1.0.3 lib/dry/logger/formatters/json.rb
dry-logger-1.0.2 lib/dry/logger/formatters/json.rb
dry-logger-1.0.1 lib/dry/logger/formatters/json.rb
dry-logger-1.0.0 lib/dry/logger/formatters/json.rb