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

Delegates all not configured calls to the internal and the additional logger.



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

def method_missing(method, *args)
  @internal_logger.send(method, *args)
  @additional_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

#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

#respond_to_missing?(method, *_args) ⇒ Boolean

Registers all methods to which the internal logger responds.

Returns:

  • (Boolean)


52
53
54
55
# File 'lib/grafana_reporter/logger/two_way_delegate_logger.rb', line 52

def respond_to_missing?(method, *_args)
  super
  @internal_logger.respond_to?(method)
end