Class: GrafanaReporter::Logger::TwoWayDelegateLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/grafana_reporter/logger/two_way_delegate_logger.rb

Overview

This logger enables a special use case, so that one and the same log will automatically be send to two different logger destinations.

One destination is the set #additional_logger= which respects the configured severity. The other destination is an internal logger, which will always log all messages in mode Logger::Severity::Debug. All messages of the internal logger can easily be retrieved, by using the #internal_messages method.

Except the #level= setting, all calls to the logger will immediately be delegated to the internal logger and the configured #additional_logger=. By having this behavior, the class can be used wherever the standard Logger can also be used.

Instance Method Summary collapse

Constructor Details

#initializeTwoWayDelegateLogger

Returns a new instance of TwoWayDelegateLogger.



20
21
22
23
24
25
# File 'lib/grafana_reporter/logger/two_way_delegate_logger.rb', line 20

def initialize
  @internal_messages = StringIO.new
  @internal_logger = ::Logger.new(@internal_messages)
  @internal_logger.level = ::Logger::Severity::DEBUG
  @additional_logger = ::Logger.new(nil)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

Registers all methods to which the internal logger will respond.



71
72
73
# File 'lib/grafana_reporter/logger/two_way_delegate_logger.rb', line 71

def method_missing(method, *args)
  @internal_logger.send(method, *args)
end

Instance Method Details

#additional_logger=(logger) ⇒ Object

Used to set the additional logger in this class to an already existing logger.

Parameters:

  • logger (Logger)

    sets the additional logger to the given value.



41
42
43
# File 'lib/grafana_reporter/logger/two_way_delegate_logger.rb', line 41

def additional_logger=(logger)
  @additional_logger = logger || ::Logger.new(nil)
end

#debug(*args) ⇒ Object



65
66
67
68
# File 'lib/grafana_reporter/logger/two_way_delegate_logger.rb', line 65

def debug(*args)
  @internal_logger.debug(*args)
  @additional_logger.debug(*args)
end

#error(*args) ⇒ Object



50
51
52
53
# File 'lib/grafana_reporter/logger/two_way_delegate_logger.rb', line 50

def error(*args)
  @internal_logger.error(*args)
  @additional_logger.error(*args)
end

#fatal(*args) ⇒ Object



45
46
47
48
# File 'lib/grafana_reporter/logger/two_way_delegate_logger.rb', line 45

def fatal(*args)
  @internal_logger.fatal(*args)
  @additional_logger.fatal(*args)
end

#info(*args) ⇒ Object



60
61
62
63
# File 'lib/grafana_reporter/logger/two_way_delegate_logger.rb', line 60

def info(*args)
  @internal_logger.info(*args)
  @additional_logger.info(*args)
end

#internal_messagesString

Returns all messages of the internal logger.

Returns:

  • (String)

    all messages of the internal logger.



34
35
36
# File 'lib/grafana_reporter/logger/two_way_delegate_logger.rb', line 34

def internal_messages
  @internal_messages.string
end

#level=(severity) ⇒ Object

Sets the severity level of the additional logger to the given severity.

Parameters:

  • severity

    one of Logger::Severity



29
30
31
# File 'lib/grafana_reporter/logger/two_way_delegate_logger.rb', line 29

def level=(severity)
  @additional_logger.level = severity
end

#warn(*args) ⇒ Object



55
56
57
58
# File 'lib/grafana_reporter/logger/two_way_delegate_logger.rb', line 55

def warn(*args)
  @internal_logger.warn(*args)
  @additional_logger.warn(*args)
end