Sha256: e1909b1b4906884513baf0e20aa6dbe379c5918be73ec8ed29f175725ff32475

Contents?: true

Size: 1.46 KB

Versions: 3

Compression:

Stored size: 1.46 KB

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

    # Define all common logging methods within Multilogger to avoid NoMethodError
    def debug(*args, &block); call_loggers(:debug, *args, &block); end

    def info(*args, &block); call_loggers(:info, *args, &block); end

    def warn(*args, &block); call_loggers(:warn, *args, &block); end

    def error(*args, &block); call_loggers(:error, *args, &block); end

    def fatal(*args, &block); call_loggers(:fatal, *args, &block); end

    def write(*args); call_loggers(:write, *args); end

    def respond_to?(method, include_all=false)
      @loggers.all? { |logger| logger.respond_to?(method, include_all) }
    end

    def call_loggers(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

    def method_missing(method, *args, &block)
      call_loggers(method, *args, &block)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
lorekeeper-2.6.4 lib/lorekeeper/multi_logger.rb
lorekeeper-2.6.3 lib/lorekeeper/multi_logger.rb
lorekeeper-2.6.2 lib/lorekeeper/multi_logger.rb