Class: MultiLogger
- Inherits:
-
Object
- Object
- MultiLogger
- Defined in:
- lib/nucleus/core/common/logging/multi_logger.rb
Overview
The MultiLogger allows to log messages not only to a file OR the stdout, but to both or even more loggers at the same time. The severity defaults to WARN but can be specified when instantiating the MultiLogger.
log_1 = Logger.new(STDOUT)
log_2 = Logger.new(File.open('/tmp/foo'))
multi_logger = MultiLogger.new(:level => Logger::WARN, :loggers => log_1)
multi_logger.add_logger(log_2)
multi_logger.warn('Something interesting happened.')
By Chris Lowder, see gist.github.com/clowder/3639600
Instance Attribute Summary collapse
-
#level ⇒ Object
Returns the value of attribute level.
Instance Method Summary collapse
-
#add_logger(logger) ⇒ Object
Add a logger to the MultiLogger and adjust its level to the MultiLogger’s current level.
-
#close ⇒ Object
Close each Logger of the MultiLogger instance.
-
#initialize(args = {}) ⇒ Object
constructor
Initialize the MultiLogger, specify the severity level for all loggers and add one or more loggers.
Constructor Details
#initialize(args = {}) ⇒ Object
Initialize the MultiLogger, specify the severity level for all loggers and add one or more loggers.
22 23 24 25 26 27 |
# File 'lib/nucleus/core/common/logging/multi_logger.rb', line 22 def initialize(args = {}) @level = args[:level] || Logger::Severity::WARN @loggers = [] Array(args[:loggers]).each { |logger| add_logger(logger) } end |
Instance Attribute Details
#level ⇒ Object
Returns the value of attribute level.
13 14 15 |
# File 'lib/nucleus/core/common/logging/multi_logger.rb', line 13 def level @level end |
Instance Method Details
#add_logger(logger) ⇒ Object
Add a logger to the MultiLogger and adjust its level to the MultiLogger’s current level.
32 33 34 35 |
# File 'lib/nucleus/core/common/logging/multi_logger.rb', line 32 def add_logger(logger) logger.level = level @loggers << logger end |
#close ⇒ Object
Close each Logger of the MultiLogger instance
46 47 48 |
# File 'lib/nucleus/core/common/logging/multi_logger.rb', line 46 def close @loggers.map(&:close) end |