Class: ActiveSupport::BroadcastLogger
- Includes:
- LoggerSilence
- Defined in:
- lib/active_support/broadcast_logger.rb
Overview
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.
Instance Attribute Summary collapse
-
#broadcasts ⇒ Object
readonly
Returns all the logger that are part of this broadcast.
-
#formatter ⇒ Object
Returns the value of attribute formatter.
-
#progname ⇒ Object
Returns the value of attribute progname.
Instance Method Summary collapse
- #<<(message) ⇒ Object
- #add(*args, &block) ⇒ Object (also: #log)
-
#broadcast_to(*loggers) ⇒ Object
Add logger(s) to the broadcast.
- #close ⇒ Object
- #debug(*args, &block) ⇒ Object
-
#debug! ⇒ Object
Sets the log level to Logger::DEBUG for the whole broadcast.
-
#debug? ⇒ Boolean
True
if the log level allows entries with severity Logger::DEBUG to be written to at least one broadcast. - #error(*args, &block) ⇒ Object
-
#error! ⇒ Object
Sets the log level to Logger::ERROR for the whole broadcast.
-
#error? ⇒ Boolean
True
if the log level allows entries with severity Logger::ERROR to be written to at least one broadcast. - #fatal(*args, &block) ⇒ Object
-
#fatal! ⇒ Object
Sets the log level to Logger::FATAL for the whole broadcast.
-
#fatal? ⇒ Boolean
True
if the log level allows entries with severity Logger::FATAL to be written to at least one broadcast. - #info(*args, &block) ⇒ Object
-
#info! ⇒ Object
Sets the log level to Logger::INFO for the whole broadcast.
-
#info? ⇒ Boolean
True
if the log level allows entries with severity Logger::INFO to be written to at least one broadcast. -
#initialize(*loggers) ⇒ BroadcastLogger
constructor
A new instance of BroadcastLogger.
- #level ⇒ Object
- #level=(level) ⇒ Object (also: #sev_threshold=)
- #local_level=(level) ⇒ Object
-
#stop_broadcasting_to(logger) ⇒ Object
Remove a logger from the broadcast.
- #unknown(*args, &block) ⇒ Object
- #warn(*args, &block) ⇒ Object
-
#warn! ⇒ Object
Sets the log level to Logger::WARN for the whole broadcast.
-
#warn? ⇒ Boolean
True
if the log level allows entries with severity Logger::WARN to be written to at least one broadcast.
Methods included from LoggerSilence
Methods included from Concern
#append_features, #class_methods, extended, #included, #prepend_features, #prepended
Constructor Details
#initialize(*loggers) ⇒ BroadcastLogger
Returns a new instance of BroadcastLogger.
62 63 64 65 66 67 |
# File 'lib/active_support/broadcast_logger.rb', line 62 def initialize(*loggers) @broadcasts = [] @progname = "Broadcast" broadcast_to(*loggers) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object (private)
206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/active_support/broadcast_logger.rb', line 206 def method_missing(name, *args) loggers = @broadcasts.select { |logger| logger.respond_to?(name) } if loggers.none? super(name, *args) elsif loggers.one? loggers.first.send(name, *args) else loggers.map { |logger| logger.send(name, *args) } end end |
Instance Attribute Details
#broadcasts ⇒ Object (readonly)
Returns all the logger that are part of this broadcast.
58 59 60 |
# File 'lib/active_support/broadcast_logger.rb', line 58 def broadcasts @broadcasts end |
#formatter ⇒ Object
Returns the value of attribute formatter.
59 60 61 |
# File 'lib/active_support/broadcast_logger.rb', line 59 def formatter @formatter end |
#progname ⇒ Object
Returns the value of attribute progname.
60 61 62 |
# File 'lib/active_support/broadcast_logger.rb', line 60 def progname @progname end |
Instance Method Details
#<<(message) ⇒ Object
92 93 94 |
# File 'lib/active_support/broadcast_logger.rb', line 92 def <<() dispatch { |logger| logger.<<() } end |
#add(*args, &block) ⇒ Object Also known as: log
96 97 98 |
# File 'lib/active_support/broadcast_logger.rb', line 96 def add(*args, &block) dispatch { |logger| logger.add(*args, &block) } end |
#broadcast_to(*loggers) ⇒ Object
Add logger(s) to the broadcast.
broadcast_logger = ActiveSupport::BroadcastLogger.new
broadcast_logger.broadcast_to(Logger.new(STDOUT), Logger.new(STDERR))
73 74 75 |
# File 'lib/active_support/broadcast_logger.rb', line 73 def broadcast_to(*loggers) @broadcasts.concat(loggers) end |
#close ⇒ Object
142 143 144 |
# File 'lib/active_support/broadcast_logger.rb', line 142 def close dispatch { |logger| logger.close } end |
#debug(*args, &block) ⇒ Object
101 102 103 |
# File 'lib/active_support/broadcast_logger.rb', line 101 def debug(*args, &block) dispatch { |logger| logger.debug(*args, &block) } end |
#debug! ⇒ Object
Sets the log level to Logger::DEBUG for the whole broadcast.
153 154 155 |
# File 'lib/active_support/broadcast_logger.rb', line 153 def debug! dispatch { |logger| logger.debug! } end |
#debug? ⇒ Boolean
True
if the log level allows entries with severity Logger::DEBUG to be written to at least one broadcast. False
otherwise.
148 149 150 |
# File 'lib/active_support/broadcast_logger.rb', line 148 def debug? @broadcasts.any? { |logger| logger.debug? } end |
#error(*args, &block) ⇒ Object
113 114 115 |
# File 'lib/active_support/broadcast_logger.rb', line 113 def error(*args, &block) dispatch { |logger| logger.error(*args, &block) } end |
#error! ⇒ Object
Sets the log level to Logger::ERROR for the whole broadcast.
186 187 188 |
# File 'lib/active_support/broadcast_logger.rb', line 186 def error! dispatch { |logger| logger.error! } end |
#error? ⇒ Boolean
True
if the log level allows entries with severity Logger::ERROR to be written to at least one broadcast. False
otherwise.
181 182 183 |
# File 'lib/active_support/broadcast_logger.rb', line 181 def error? @broadcasts.any? { |logger| logger.error? } end |
#fatal(*args, &block) ⇒ Object
117 118 119 |
# File 'lib/active_support/broadcast_logger.rb', line 117 def fatal(*args, &block) dispatch { |logger| logger.fatal(*args, &block) } end |
#fatal! ⇒ Object
Sets the log level to Logger::FATAL for the whole broadcast.
197 198 199 |
# File 'lib/active_support/broadcast_logger.rb', line 197 def fatal! dispatch { |logger| logger.fatal! } end |
#fatal? ⇒ Boolean
True
if the log level allows entries with severity Logger::FATAL to be written to at least one broadcast. False
otherwise.
192 193 194 |
# File 'lib/active_support/broadcast_logger.rb', line 192 def fatal? @broadcasts.any? { |logger| logger.fatal? } end |
#info(*args, &block) ⇒ Object
105 106 107 |
# File 'lib/active_support/broadcast_logger.rb', line 105 def info(*args, &block) dispatch { |logger| logger.info(*args, &block) } end |
#info! ⇒ Object
Sets the log level to Logger::INFO for the whole broadcast.
164 165 166 |
# File 'lib/active_support/broadcast_logger.rb', line 164 def info! dispatch { |logger| logger.info! } end |
#info? ⇒ Boolean
True
if the log level allows entries with severity Logger::INFO to be written to at least one broadcast. False
otherwise.
159 160 161 |
# File 'lib/active_support/broadcast_logger.rb', line 159 def info? @broadcasts.any? { |logger| logger.info? } end |
#level ⇒ Object
88 89 90 |
# File 'lib/active_support/broadcast_logger.rb', line 88 def level @broadcasts.map(&:level).min end |
#level=(level) ⇒ Object Also known as: sev_threshold=
131 132 133 |
# File 'lib/active_support/broadcast_logger.rb', line 131 def level=(level) dispatch { |logger| logger.level = level } end |
#local_level=(level) ⇒ Object
136 137 138 139 140 |
# File 'lib/active_support/broadcast_logger.rb', line 136 def local_level=(level) dispatch do |logger| logger.local_level = level if logger.respond_to?(:local_level=) end end |
#stop_broadcasting_to(logger) ⇒ Object
Remove a logger from the broadcast. When a logger is removed, messages sent to the broadcast will no longer be written to its sink.
sink = Logger.new(STDOUT)
broadcast_logger = ActiveSupport::BroadcastLogger.new
broadcast_logger.stop_broadcasting_to(sink)
84 85 86 |
# File 'lib/active_support/broadcast_logger.rb', line 84 def stop_broadcasting_to(logger) @broadcasts.delete(logger) end |
#unknown(*args, &block) ⇒ Object
121 122 123 |
# File 'lib/active_support/broadcast_logger.rb', line 121 def unknown(*args, &block) dispatch { |logger| logger.unknown(*args, &block) } end |
#warn(*args, &block) ⇒ Object
109 110 111 |
# File 'lib/active_support/broadcast_logger.rb', line 109 def warn(*args, &block) dispatch { |logger| logger.warn(*args, &block) } end |
#warn! ⇒ Object
Sets the log level to Logger::WARN for the whole broadcast.
175 176 177 |
# File 'lib/active_support/broadcast_logger.rb', line 175 def warn! dispatch { |logger| logger.warn! } end |
#warn? ⇒ Boolean
True
if the log level allows entries with severity Logger::WARN to be written to at least one broadcast. False
otherwise.
170 171 172 |
# File 'lib/active_support/broadcast_logger.rb', line 170 def warn? @broadcasts.any? { |logger| logger.warn? } end |