Sha256: 3c17f66d7f6565fd1ef737f557e58c4f7fa0f0943588c63980ea39dde4c0314a

Contents?: true

Size: 878 Bytes

Versions: 2

Compression:

Stored size: 878 Bytes

Contents

# frozen_string_literal: true

module Lorekeeper
  # Allows to create a logger that will pass information to any logger registered
  # It is useful so send the same message through different loggers to different sinks
  class MultiLogger
    def initialize
      @loggers = []
    end

    def add_logger(logger)
      @loggers << logger
    end

    def inspect
      "Lorekeeper multilogger, loggers: #{@loggers.map(&:inspect)}"
    end

    def respond_to?(method)
      @loggers.all? { |logger| logger.respond_to?(method) }
    end

    def method_missing(method, *args, &block)
      result = @loggers.map do |logger|
        logger.public_send(method, *args, &block) if logger.respond_to?(method)
      end
      # We call all the methods, delete nils and duplicates.
      # Then hope for the best taking the first value
      result.compact.uniq.first
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
lorekeeper-1.7.1 lib/lorekeeper/multi_logger.rb
lorekeeper-1.7.0 lib/lorekeeper/multi_logger.rb