Class: RTP::Logging::ClassMethods::ProxyLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/rtp-connect/logging.rb

Overview

We use our own ProxyLogger to achieve the features wanted for RTP logging, e.g. using RTP as progname for messages logged within the RTP module (for both the Standard logger as well as the Rails logger), while still allowing a custom progname to be used when the logger is called outside the RTP module.

Instance Method Summary collapse

Constructor Details

#initialize(target) ⇒ ProxyLogger

Creating the ProxyLogger instance.

Parameters:

  • target (Logger)

    a logger instance (e.g. Standard Logger or ActiveSupport::BufferedLogger)



57
58
59
# File 'lib/rtp-connect/logging.rb', line 57

def initialize(target)
  @target = target
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object

Catches missing methods.

In our case, the methods of interest are the typical logger methods, i.e. log, info, fatal, error, debug, where the arguments/block are redirected to the logger in a specific way so that our stated logger features are achieved (this behaviour depends on the logger (Rails vs Standard) and in the case of Standard logger, whether or not a block is given).

Examples:

Inside the RTP module or an external class with ‘include RTP::Logging’:

logger.info "message"

Calling from outside the RTP module:

RTP.logger.info "message"


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/rtp-connect/logging.rb', line 76

def method_missing(method_name, *args, &block)
  if method_name.to_s =~ /(log|debug|info|warn|error|fatal)/
    # Rails uses it's own buffered logger which does not
    # work with progname + block as the standard logger does:
    if defined?(Rails)
      @target.send(method_name, "RTP: #{args.first}")
    elsif block_given?
      @target.send(method_name, *args) { yield }
    else
      @target.send(method_name, "RTP") { args.first }
    end
  else
    @target.send(method_name, *args, &block)
  end
end