Class: Lorekeeper::MultiLogger
- Inherits:
-
Object
- Object
- Lorekeeper::MultiLogger
show all
- Defined in:
- lib/lorekeeper/multi_logger.rb
Overview
Allows to create a logger that will pass information to any logger registered It is useful so send the same message through different loggers to different sinks
Instance Method Summary
collapse
-
#add_logger(logger) ⇒ Object
-
#call_loggers(method, *args, &block) ⇒ Object
-
#debug(*args, &block) ⇒ Object
Define all common logging methods within Multilogger to avoid NoMethodError.
-
#debug_with_data(*args, &block) ⇒ Object
-
#error(*args, &block) ⇒ Object
-
#error_with_data(*args, &block) ⇒ Object
-
#fatal(*args, &block) ⇒ Object
-
#fatal_with_data(*args, &block) ⇒ Object
-
#info(*args, &block) ⇒ Object
-
#info_with_data(*args, &block) ⇒ Object
-
#initialize ⇒ MultiLogger
constructor
A new instance of MultiLogger.
-
#inspect ⇒ Object
-
#method_missing(method, *args, &block) ⇒ Object
-
#respond_to?(method, all_included: false) ⇒ Boolean
-
#warn(*args, &block) ⇒ Object
-
#warn_with_data(*args, &block) ⇒ Object
-
#write(*args) ⇒ Object
Constructor Details
Returns a new instance of MultiLogger.
7
8
9
|
# File 'lib/lorekeeper/multi_logger.rb', line 7
def initialize
@loggers = []
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
55
56
57
|
# File 'lib/lorekeeper/multi_logger.rb', line 55
def method_missing(method, *args, &block)
call_loggers(method, *args, &block)
end
|
Instance Method Details
#add_logger(logger) ⇒ Object
11
12
13
|
# File 'lib/lorekeeper/multi_logger.rb', line 11
def add_logger(logger)
@loggers << logger
end
|
#call_loggers(method, *args, &block) ⇒ Object
46
47
48
49
50
51
52
53
|
# File 'lib/lorekeeper/multi_logger.rb', line 46
def call_loggers(method, *args, &block)
result = @loggers.map do |logger|
logger.public_send(method, *args, &block) if logger.respond_to?(method)
end
result.compact.uniq.first
end
|
#debug(*args, &block) ⇒ Object
Define all common logging methods within Multilogger to avoid NoMethodError
20
|
# File 'lib/lorekeeper/multi_logger.rb', line 20
def debug(*args, &block); call_loggers(:debug, *args, &block); end
|
#debug_with_data(*args, &block) ⇒ Object
22
|
# File 'lib/lorekeeper/multi_logger.rb', line 22
def debug_with_data(*args, &block); call_loggers(:debug, *args, &block); end
|
#error(*args, &block) ⇒ Object
32
|
# File 'lib/lorekeeper/multi_logger.rb', line 32
def error(*args, &block); call_loggers(:error, *args, &block); end
|
#error_with_data(*args, &block) ⇒ Object
34
|
# File 'lib/lorekeeper/multi_logger.rb', line 34
def error_with_data(*args, &block); call_loggers(:error, *args, &block); end
|
#fatal(*args, &block) ⇒ Object
36
|
# File 'lib/lorekeeper/multi_logger.rb', line 36
def fatal(*args, &block); call_loggers(:fatal, *args, &block); end
|
#fatal_with_data(*args, &block) ⇒ Object
38
|
# File 'lib/lorekeeper/multi_logger.rb', line 38
def fatal_with_data(*args, &block); call_loggers(:fatal, *args, &block); end
|
#info(*args, &block) ⇒ Object
24
|
# File 'lib/lorekeeper/multi_logger.rb', line 24
def info(*args, &block); call_loggers(:info, *args, &block); end
|
#info_with_data(*args, &block) ⇒ Object
26
|
# File 'lib/lorekeeper/multi_logger.rb', line 26
def info_with_data(*args, &block); call_loggers(:info, *args, &block); end
|
#inspect ⇒ Object
15
16
17
|
# File 'lib/lorekeeper/multi_logger.rb', line 15
def inspect
"Lorekeeper multilogger, loggers: #{@loggers.map(&:inspect)}"
end
|
#respond_to?(method, all_included: false) ⇒ Boolean
42
43
44
|
# File 'lib/lorekeeper/multi_logger.rb', line 42
def respond_to?(method, all_included: false)
@loggers.all? { |logger| logger.respond_to?(method, all_included) }
end
|
#warn(*args, &block) ⇒ Object
28
|
# File 'lib/lorekeeper/multi_logger.rb', line 28
def warn(*args, &block); call_loggers(:warn, *args, &block); end
|
#warn_with_data(*args, &block) ⇒ Object
30
|
# File 'lib/lorekeeper/multi_logger.rb', line 30
def warn_with_data(*args, &block); call_loggers(:warn, *args, &block); end
|
#write(*args) ⇒ Object
40
|
# File 'lib/lorekeeper/multi_logger.rb', line 40
def write(*args); call_loggers(:write, *args); end
|