# frozen_string_literal: true module Kanal module Core module Logging class CompositeLogger # Property allowed_methods defines which methods can be called. # These methods mirror standard Logger methods. def initialize @loggers = [] @allowed_methods = [:debug, :info, :warn, :error, :fatal, :unknown] end def add_logger(logger) @loggers << logger end # This method gets called when calling any method in @allowed_methods. # It passes message string to method call of every added logger. # # @param method [Symbol] # @param message [String] def method_missing(method, message) # Getting caller class name to put into log message later. begin caller_class_name = "[" + caller[0].split(".rb").first.split("/").last.split('_').collect(&:capitalize).join + "]" rescue caller_class_name = "[ErrorGettingClassName]" end raise "Wrong method called for CompositeLogger" unless @allowed_methods.include? method @loggers.each do |logger| # progname is a property of standard Logger. It is a prefix for log message. # In our case progname is used to store class name of class which calls for CompositeLogger methods. logger.progname = caller_class_name logger.send method, message end end end end end end