Class: Securial::Logger::Broadcaster
- Inherits:
-
Object
- Object
- Securial::Logger::Broadcaster
- 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.
Instance Method Summary collapse
-
#<<(msg) ⇒ Array
Writes a message to each underlying logger.
-
#close ⇒ void
Closes all underlying loggers.
-
#formatter ⇒ nil
Returns nil to satisfy the Logger interface.
-
#formatter=(_formatter) ⇒ void
No-op method to satisfy the Logger interface.
-
#initialize(loggers) ⇒ Broadcaster
constructor
Initializes a new broadcaster with the provided loggers.
-
#loggers ⇒ Array<Logger>
Returns the collection of managed loggers.
-
#method_missing(method, *args, &block) ⇒ Array
Delegates missing methods to all underlying loggers.
-
#respond_to_missing?(method, include_private = false) ⇒ Boolean
Checks if the broadcaster responds to the given method.
-
#tagged(*tags) { ... } ⇒ Object
Executes a block with the specified tags added to the log.
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.
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 |
#close ⇒ void
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 |
#formatter ⇒ nil
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 |
#loggers ⇒ Array<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.
107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/securial/logger/broadcaster.rb', line 107 def tagged(*, &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(*, &blk) } end.call else yield end end |