- 1.0.0
- 1.1.6
- 1.2.6
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.8
- 3.0.0
- 3.0.9
- 3.1.0
- 3.2.1
- 3.2.8
- 3.2.13
- 4.0.2
- 4.1.8
- 4.2.1
- 4.2.7
- 4.2.9
- 5.0.0.1
- 5.1.7
- 5.2.3
- 6.0.0
- 6.1.3.1
- 6.1.7.7
- 7.0.0
- 7.1.3.2 (0)
- 7.1.3.4 (0)
- What's this?
Active Support Broadcast Logger
The Broadcast logger is a logger used to write messages to multiple IO. It is commonly used in development to display messages on STDOUT and also write them to a file (development.log). With the Broadcast logger, you can broadcast your logs to a unlimited number of sinks.
The BroadcastLogger acts as a standard logger and all methods you are used to are available. However, all the methods on this logger will propagate and be delegated to the other loggers that are part of the broadcast.
Broadcasting your logs.
stdout_logger = Logger.new(STDOUT) file_logger = Logger.new("development.log") broadcast = BroadcastLogger.new(stdout_logger, file_logger) broadcast.info("Hello world!") # Writes the log to STDOUT and the development.log file.
Add a logger to the broadcast.
stdout_logger = Logger.new(STDOUT) broadcast = BroadcastLogger.new(stdout_logger) file_logger = Logger.new("development.log") broadcast.broadcast_to(file_logger) broadcast.info("Hello world!") # Writes the log to STDOUT and the development.log file.
Modifying the log level for all broadcasted loggers.
stdout_logger = Logger.new(STDOUT) file_logger = Logger.new("development.log") broadcast = BroadcastLogger.new(stdout_logger, file_logger) broadcast.level = Logger::FATAL # Modify the log level for the whole broadcast.
Stop broadcasting log to a sink.
stdout_logger = Logger.new(STDOUT) file_logger = Logger.new("development.log") broadcast = BroadcastLogger.new(stdout_logger, file_logger) broadcast.info("Hello world!") # Writes the log to STDOUT and the development.log file. broadcast.stop_broadcasting_to(file_logger) broadcast.info("Hello world!") # Writes the log *only* to STDOUT.
At least one sink has to be part of the broadcast. Otherwise, your logs will not be written anywhere. For instance:
broadcast = BroadcastLogger.new broadcast.info("Hello world") # The log message will appear nowhere.
If you are adding a custom logger with custom methods to the broadcast, the `BroadcastLogger` will proxy them and return the raw value, or an array of raw values, depending on how many loggers in the broadcasts responded to the method:
class MyLogger < ::Logger def loggable? true end end logger = BroadcastLogger.new logger.loggable? # => A NoMethodError exception is raised because no loggers in the broadcasts could respond. logger.broadcast_to(MyLogger.new(STDOUT)) logger.loggable? # => true logger.broadcast_to(MyLogger.new(STDOUT)) puts logger.broadcasts # => [MyLogger, MyLogger] logger.loggable? # [true, true]