Class: CemAcpt::Logging::MultiLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/cem_acpt/logging.rb

Overview

Delegator class for when you want to log to multiple devices at the same time, such as STDOUT and a file.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*logger_instances) ⇒ MultiLogger

Returns a new instance of MultiLogger.



40
41
42
# File 'lib/cem_acpt/logging.rb', line 40

def initialize(*logger_instances)
  @loggers = logger_instances
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/cem_acpt/logging.rb', line 48

def method_missing(m, *args, &block)
  if loggers.all? { |l| l.respond_to?(m) }
    loggers.map { |l| l.send(m, *args, &block) }
  else
    super
  end
end

Class Method Details

.from_logger_configs(configs) ⇒ MultiLogger

Returns a new instance of MultiLogger.

Parameters:

  • configs (Array<Hash>)

    an array of hashes containing the configuration for each logger. Each hash must contain the keys :logdev, :shift_age, and :shift_size. The values for these keys are passed to the Logger.new method. Any other keys and values are passed to the Logger.new method as keyword arguments.

Returns:



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/cem_acpt/logging.rb', line 28

def self.from_logger_configs(configs)
  return if configs.nil? || configs.empty?

  logger_instances = configs.map do |config|
    CemAcpt::Logging::Logger.new(config[:logdev],
                                 config[:shift_age],
                                 config[:shift_size],
                                 **config.reject { |k, _| [:logdev, :shift_age, :shift_size].include?(k) })
  end
  new(*logger_instances)
end

Instance Method Details

#logger_configsObject



44
45
46
# File 'lib/cem_acpt/logging.rb', line 44

def logger_configs
  loggers.map(&:config_hash)
end

#respond_to_missing?(m, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/cem_acpt/logging.rb', line 56

def respond_to_missing?(m, include_private = false)
  loggers.all? { |l| l.respond_to?(m) } || super
end