Class: Securial::Logger::Broadcaster

Inherits:
Object
  • Object
show all
Defined in:
lib/securial/logger/broadcaster.rb

Overview

Broadcasts log messages to multiple logger instances simultaneously.

This class implements the Logger interface and forwards all logging calls to a collection of underlying loggers. This allows unified logging to multiple destinations (e.g., file and stdout) while maintaining specific formatting for each destination.

Examples:

Using a broadcaster with two loggers

file_logger = Logger.new('app.log')
console_logger = Logger.new(STDOUT)
broadcaster = Broadcaster.new([file_logger, console_logger])

# This logs to both destinations
broadcaster.info("User logged in")

Instance Method Summary collapse

Constructor Details

#initialize(loggers) ⇒ Broadcaster

Initializes a new broadcaster with the provided loggers.



42
43
44
# File 'lib/securial/logger/broadcaster.rb', line 42

def initialize(loggers)
  @loggers = loggers
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Array

Delegates missing methods to all underlying loggers.

If all loggers respond to the method, it will be called on each logger. Otherwise, raises a NoMethodError.

Raises:

  • (NoMethodError)

    If any logger doesn’t respond to the method



139
140
141
142
143
144
145
# File 'lib/securial/logger/broadcaster.rb', line 139

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

Instance Method Details

#<<(msg) ⇒ Array

Writes a message to each underlying logger.



58
59
60
# File 'lib/securial/logger/broadcaster.rb', line 58

def <<(msg)
  @loggers.each { |logger| logger << msg }
end

#closevoid

This method returns an undefined value.

Closes all underlying loggers.



65
66
67
# File 'lib/securial/logger/broadcaster.rb', line 65

def close
  @loggers.each(&:close)
end

#formatternil

Returns nil to satisfy the Logger interface.

Since each underlying logger has its own formatter, there is no single formatter for the broadcaster.



86
87
88
# File 'lib/securial/logger/broadcaster.rb', line 86

def formatter
  nil
end

#formatter=(_formatter) ⇒ void

This method returns an undefined value.

No-op method to satisfy the Logger interface.

Since each underlying logger has its own formatter, setting a formatter on the broadcaster is not supported.



76
77
78
# File 'lib/securial/logger/broadcaster.rb', line 76

def formatter=(_formatter)
  # Do nothing - each logger maintains its own formatter
end

#loggersArray<Logger>

Returns the collection of managed loggers.



94
95
96
# File 'lib/securial/logger/broadcaster.rb', line 94

def loggers
  @loggers
end

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

Checks if the broadcaster responds to the given method.



125
126
127
# File 'lib/securial/logger/broadcaster.rb', line 125

def respond_to_missing?(method, include_private = false)
  @loggers.any? { |logger| logger.respond_to?(method, include_private) } || super
end

#tagged(*tags) { ... } ⇒ Object

Executes a block with the specified tags added to the log.

Supports ActiveSupport::TaggedLogging by forwarding tagged blocks to all underlying loggers that support tagging.

Yields:

  • Block to be executed with the tags applied



107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/securial/logger/broadcaster.rb', line 107

def tagged(*tags, &block)
  # If all loggers support tagged, nest the calls, otherwise just yield.
  taggable_loggers = @loggers.select { |logger| logger.respond_to?(:tagged) }
  if taggable_loggers.any?
    # Nest tags for all taggable loggers
    taggable_loggers.reverse.inject(block) do |blk, logger|
      proc { logger.tagged(*tags, &blk) }
    end.call
  else
    yield
  end
end