Class: Tlogger::LoggerGroup
- Inherits:
-
Object
- Object
- Tlogger::LoggerGroup
- Defined in:
- lib/tlogger/logger_group.rb
Overview
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
Instance Method Summary collapse
-
#add_logger(key, logger) ⇒ Object
Add an existing logger instance to this group.
-
#close ⇒ Object
Close the logger group, effectively close all registered loggers.
-
#create_logger(key, *params) ⇒ Object
Create and add the logger into the group and registerd it with the given
key
. -
#delete_logger(key) ⇒ Object
Delete this logger from the group.
-
#detach_logger(key) ⇒ Object
Detach the logger from the group, but not close the logger.
-
#get_log(key) ⇒ Object
Returns the logger that registered to the
key
. -
#initialize ⇒ LoggerGroup
constructor
A new instance of LoggerGroup.
-
#method_missing(mtd, *args, &block) ⇒ Object
Delegate unknown method to the underlying logger.
Constructor Details
#initialize ⇒ LoggerGroup
Returns a new instance of LoggerGroup.
17 18 19 |
# File 'lib/tlogger/logger_group.rb', line 17 def initialize @loggers = { } end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(mtd, *args, &block) ⇒ Object
Delegate unknown method to the underlying logger
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# 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. end end #super if not hit end |
Instance Method Details
#add_logger(key, logger) ⇒ Object
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.
69 70 71 |
# File 'lib/tlogger/logger_group.rb', line 69 def add_logger(key, logger) @loggers[key] = logger end |
#close ⇒ Object
Close the logger group, effectively close all registered loggers
79 80 81 82 83 84 85 |
# File 'lib/tlogger/logger_group.rb', line 79 def close @loggers.each do |k,v| v.close v = nil end @loggers.clear end |
#create_logger(key, *params) ⇒ Object
Create and add the logger into the group and registerd it with the given key
*params shall be passed to underlying Tlogger new method
Returns created Tlogger object
27 28 29 30 |
# File 'lib/tlogger/logger_group.rb', line 27 def create_logger(key, *params) @loggers[key] = Tlogger.new(*params) @loggers[key] end |
#delete_logger(key) ⇒ Object
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.
37 38 39 40 41 42 43 44 |
# 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_logger(key) ⇒ Object
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
54 55 56 |
# File 'lib/tlogger/logger_group.rb', line 54 def detach_logger(key) @loggers.delete(key) end |
#get_log(key) ⇒ Object
Returns the logger that registered to the key
74 75 76 |
# File 'lib/tlogger/logger_group.rb', line 74 def get_log(key) @loggers[key] end |