Sha256: 60d8a07754e57432a6e7801f1fcd67e2170263d707f34ae924903034b530cec9

Contents?: true

Size: 1 KB

Versions: 5

Compression:

Stored size: 1 KB

Contents

# frozen_string_literal: true

require 'logger'
require 'forwardable'

module MrLogaLoga
  # == Description
  #
  # A proxy that attaches contextual information to the underlying logger when called.
  #
  # @api private
  class LoggerProxy
    extend Forwardable

    def initialize(logger, context_proc)
      @logger = logger
      @context_proc = context_proc
    end

    def add(severity, message = nil, progname = nil, **context, &block)
      severity ||= UNKNOWN
      return true unless @logger.log?(severity)

      context = @context_proc.call.merge(context)

      @logger.add(severity, message, progname, **context, &block)
    end

    alias log add

    %i[debug info warn error fatal unknown].each do |symbol|
      def_delegator :@logger, "#{symbol}?".to_sym, "#{symbol}?".to_sym

      define_method(symbol) do |progname = nil, **context, &block|
        severity = Object.const_get("Logger::Severity::#{symbol.to_s.upcase}")
        add(severity, nil, progname, **context, &block)
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
mr_loga_loga-0.1.5 lib/mr_loga_loga/logger_proxy.rb
mr_loga_loga-0.1.4 lib/mr_loga_loga/logger_proxy.rb
mr_loga_loga-0.1.3 lib/mr_loga_loga/logger_proxy.rb
mr_loga_loga-0.1.2 lib/mr_loga_loga/logger_proxy.rb
mr_loga_loga-0.1.1 lib/mr_loga_loga/logger_proxy.rb