Sha256: cc1f711bc0d5324e352d34c08579bbd2197e7b2495db4b99d67b2e8bcd01ccce

Contents?: true

Size: 1.85 KB

Versions: 2

Compression:

Stored size: 1.85 KB

Contents

# frozen_string_literal: true

require "logger"
require "dry/logger/filter"

module Dry
  module Logger
    module Formatters
      # Default structured formatter which receives {Logger::Entry} from the backends.
      #
      # @see http://www.ruby-doc.org/stdlib/libdoc/logger/rdoc/Logger/Formatter.html
      #
      # @since 1.0.0
      # @api public
      class Structured < ::Logger::Formatter
        # @since 1.0.0
        # @api private
        DEFAULT_FILTERS = [].freeze

        # @since 1.0.0
        # @api private
        NOOP_FILTER = -> message { message }

        # @since 1.0.0
        # @api private
        NEW_LINE = $/ # rubocop:disable Style/SpecialGlobalVars

        # @since 1.0.0
        # @api private
        attr_reader :filter

        # @since 1.0.0
        # @api private
        attr_reader :options

        # @since 1.0.0
        # @api private
        def initialize(filters: DEFAULT_FILTERS, **options)
          super()
          @filter = filters.equal?(DEFAULT_FILTERS) ? NOOP_FILTER : Filter.new(filters)
          @options = options
        end

        # Filter and then format the log entry into a string
        #
        # Custom formatters typically won't have to override this method because
        # the actual formatting logic is implemented as Structured#format
        #
        # @see http://www.ruby-doc.org/stdlib/libdoc/logger/rdoc/Logger/Formatter.html#method-i-call
        #
        # @since 1.0.0
        # @return [String]
        # @api public
        def call(_severity, _time, _progname, entry)
          format(entry.filter(filter))
        end

        # Format entry into a loggable object
        #
        # Custom formatters should override this method
        #
        # @api since 1.0.0
        # @return [Entry]
        # @api public
        def format(entry)
          entry
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dry-logger-1.0.0.rc2 lib/dry/logger/formatters/structured.rb
dry-logger-1.0.0.rc1 lib/dry/logger/formatters/structured.rb