class Tlogger::LoggerGroup
LoggerGroup
is yet at wrapper around the Tlogger
This class can be used as Tlogger
replacement as it is delegated to the Tlogger
upon method_missing
method triggered.
However this allow configuration of multiple loggers into single class.
When operation such as 'debug' is called on this class all the registered logger shall be called the 'debug' method each therefore it will be logged to all registered loggers
Public Class Methods
# File lib/tlogger/logger_group.rb, line 17 def initialize @loggers = { } end
Public Instance Methods
Add an existing logger instance to this group
Noted that this logger object not necessary to be Tlogger
object. It can be any object as long as it has the method that response to the usage.
This is due to this class just a thin wrapper around the 'logger' object that any method call unknown to local shall be redirected to the 'logger' class.
In another way, it is perfectly ok to call say_hello() on LoggerGroup
if the 'logger' given response to method say_hello() or else NoMethodException shall be thrown. It is that simple.
# File lib/tlogger/logger_group.rb, line 69 def add_logger(key, logger) @loggers[key] = logger end
Close the logger group, effectively close all registered loggers
# File lib/tlogger/logger_group.rb, line 79 def close @loggers.each do |k,v| v.close v = nil end @loggers.clear end
Delete this logger from the group.
delete_logger
different from detach_logger
as delete_logger
shall close and set the logger to nil.
detach_logger
however just remove the logger from the group and it is up to the applicaation to close it.
# File lib/tlogger/logger_group.rb, line 37 def delete_logger(key) logger = @loggers[key] if not logger.nil? logger.close logger = nil end @loggers.delete(key) end
Detach the logger from the group, but not close the logger
Detach the logger return the object to the caller and remove it from the internal group
The effect is after detach operation, any logging done to this group would not include that particular logger and application is free to use the logger to log messages
# File lib/tlogger/logger_group.rb, line 54 def detach_logger(key) @loggers.delete(key) end
Returns the logger that registered to the key
# File lib/tlogger/logger_group.rb, line 74 def get_log(key) @loggers[key] end
Delegate unknown method to the underlying logger
# File lib/tlogger/logger_group.rb, line 88 def method_missing(mtd,*args,&block) #hit = false @loggers.each do |k,v| begin v.send(mtd,*args,&block) #hit = true rescue Exception => ex STDERR.puts ex.message end end #super if not hit end