# frozen_string_literal: true require "logger" module Kanal module Core module Logging # # Object of this class us used to store and use several loggers. # It inherits from standard Logger and overrides it's methods for accepting messages for every possible log level. # Calling #debug (or any other loglevel) on this object results in calling #debug for every logger that it stores. # class CompositeLogger < ::Logger # Property allowed_methods defines which methods can be called. # These methods mirror standard Logger methods. def initialize @loggers = [] end def add_logger(logger) @loggers << logger end def debug(message) feed_message_to_loggers :debug, caller_class_name(caller.first), message end def info(message) feed_message_to_loggers :info, caller_class_name(caller.first), message end def warn(message) feed_message_to_loggers :warn, caller_class_name(caller.first), message end def error(message) feed_message_to_loggers :error, caller_class_name(caller.first), message end def fatal(message) feed_message_to_loggers :fatal, caller_class_name(caller.first), message end def unknown(message) feed_message_to_loggers :unknown, caller_class_name(caller.first), message end private def caller_class_name(caller) begin caller_class_name = "[" + caller.split(".rb").first.split("/").last.split('_').collect(&:capitalize).join + "]" rescue caller_class_name = "[ErrorGettingClassName]" end caller_class_name end def feed_message_to_loggers(method_name, caller_class_name, message) @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_name, message end end end end end end