Class: TeLogger::LoggerGroup

Inherits:
Object
  • Object
show all
Defined in:
lib/teLogger/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

Constructor Details

#initializeLoggerGroup

Returns a new instance of LoggerGroup.



17
18
19
# File 'lib/teLogger/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



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/teLogger/logger_group.rb', line 93

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

Instance Method Details

#attach_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/teLogger/logger_group.rb', line 69

def attach_logger(key, logger)
  @loggers[key] = logger
end

#closeObject

Close the logger group, effectively close all registered loggers



84
85
86
87
88
89
90
# File 'lib/teLogger/logger_group.rb', line 84

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/teLogger/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/teLogger/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/teLogger/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/teLogger/logger_group.rb', line 74

def get_log(key)
  @loggers[key]
end

#registered_loggerObject

Returns the registered logger



79
80
81
# File 'lib/teLogger/logger_group.rb', line 79

def registered_logger
  @loggers.keys.freeze
end